Files
oop/assignment5/Question4/Aircraft.java
2022-04-24 13:24:47 +08:00

128 lines
3.8 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Aircraft.java inherited from Vehicle class
*/
public class Aircraft extends Vehicle {
/**
* The altitude at which the aircraft is flying.
*/
private int altitude;
/**
* Constructor.
*
* @param speedLimit the speed limit of the aircraft
* @param speed the speed of the aircraft
* @param altitude the altitude of the aircraft
* @throws BadSpeedSetting if the speed limit is negative
* @throws BadAltitudeSetting if the altitude is negative
*/
public Aircraft(int speedLimit, int speed, int altitude) throws BadSpeedSetting, BadAltitudeSetting {
super(speedLimit, speed);
if (altitude < 0) {
throw new BadAltitudeSetting("Altitude cannot be negative!");
}
this.altitude = altitude;
}
/**
* Getter of altitude.
*
* @return the altitude of the aircraft
*/
public int getAltitude() {
return altitude;
}
/**
* Aircraft can fly.
*
* @return true
*/
@Override
public boolean canFly() {
return true;
}
/**
* Test.
*/
public static void testAircraft() {
try {
Aircraft aircraft;
try {
aircraft = new Aircraft(-10, 10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, -10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, 10, -10);
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
aircraft = new Aircraft(100, 50, 3900);
System.out.println(aircraft.getAltitude() == 3900);
System.out.println(aircraft.getSpeed() == 50);
System.out.println(aircraft.canFly() == true);
// acceleration
System.out.println(aircraft.accelerate(10) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 70);
System.out.println(aircraft.getSpeed() == 70);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 90);
System.out.println(aircraft.getSpeed() == 90);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 100);
System.out.println(aircraft.getSpeed() == 100);
System.out.println(aircraft.canFly() == true);
try {
aircraft.accelerate(10);
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
// deceleration
System.out.println(aircraft.accelerate(-20) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 40);
System.out.println(aircraft.getSpeed() == 40);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 20);
System.out.println(aircraft.getSpeed() == 20);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 0);
System.out.println(aircraft.getSpeed() == 0);
System.out.println(aircraft.canFly() == true);
try {
aircraft.accelerate(-20);
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage()
.equals("Current speed is 0, not enough speed to decelerate!"));
}
} catch (Exception e) {
System.out.println("Error uncaught exception: " + e.getMessage());
}
}
}