Skip to main content

L08 - Autopilot Interface Lab

In this lab, there is no main method, we are not running the code, just checking if it compiles without errors. To do that just make sure there are no errors.

  1. Fred Finn, ace programmer, has created an interface to ensure programmers who implement airplanes include the essential functions of an autopilot. Enter his code as shown below into a new Interface (not Class!!) named Autopilot:
public interface Autopilot { 
  final double MAX_AIRSPEED = 550; 
  double airspeed = 0; 
  double altitude = 0; 
  double heading = 0; 
  public void setAirspeed(double inAirspeed); 
  public void setAltitude(double inAltitude); 
  public void setHeading(double inHeading); 
}
  1. Make sure you've entered the code as shown and try to compile Autopilot.java. Do you get any errors? Enter your answer on the attached lab answer sheet.
  2. Sandy Smith has attempted to implement this interface in her design of an airplane. Her code is shown below. Enter this code into a class named Airliner.
public class Airliner implements Autopilot { 
  private double maxAltitude;

  public Airliner(double inMaxAltitude) {
    maxAltitude = inMaxAltitude; 
  }
  
  public void setAirspeed(double inAirspeed) { 
    airspeed = inAirspeed; 
  }
  
  public void setAltitude(double inAltitude) { 
    altitude = inAltitude; 
  } 
}
  1. Make sure you've entered the code as shown and try to compile Airliner.java. Do you get any errors? (Hint: the answer is yes). Describe the errors you encounter on your answer sheet.
  2. Describe the interface coding rule that Fred Finn violated on your answer sheet (Hint: he violated one rule three times).
  3. Fix the Autopilot interface code to correct Fred's error.
  4. Now compile Airliner.java again and see if you get any errors.
  5. Fix the Airliner code to correct Sandy's error.

Lab Answer Sheet

  1. What errors did you get when compiling Autopilot.java?
  2. Describe the type of errors encountered when Airliner.java was compiled:
  3. What interface coding rule did Fred Finn violate?
  4. Describe the error when Airliner.java was compiled: