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.
- 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);
}
- 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. - 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;
}
}
- 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. - Describe the interface coding rule that Fred Finn violated on your answer sheet (Hint: he violated one rule three times).
- Fix the
Autopilot
interface code to correct Fred's error. - Now compile
Airliner.java
again and see if you get any errors. - Fix the
Airliner
code to correct Sandy's error.
Lab Answer Sheet
- What errors did you get when compiling
Autopilot.java?
- Describe the type of errors encountered when
Airliner.java
was compiled: - What interface coding rule did Fred Finn violate?
- Describe the error when
Airliner.java
was compiled:
No Comments