diff --git a/assignment5/Question1/BadSpeedSetting.java b/assignment5/Question1/BadSpeedSetting.java new file mode 100644 index 0000000..2a08413 --- /dev/null +++ b/assignment5/Question1/BadSpeedSetting.java @@ -0,0 +1,15 @@ +/* + * The BadSpeedSetting class is used to test the speed of the program. + */ +public class BadSpeedSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadSpeedSetting.class.getName().hashCode(); + + public BadSpeedSetting() { + super(); + } + + public BadSpeedSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question1/ExceedSpeedLimit.java b/assignment5/Question1/ExceedSpeedLimit.java new file mode 100644 index 0000000..ce76da8 --- /dev/null +++ b/assignment5/Question1/ExceedSpeedLimit.java @@ -0,0 +1,15 @@ +/* + * The ExceedSpeedLimit class is used to check if the speed limit is exceeded + */ +public class ExceedSpeedLimit extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = ExceedSpeedLimit.class.getName().hashCode(); + + public ExceedSpeedLimit() { + super(); + } + + public ExceedSpeedLimit(String message) { + super(message); + } +} diff --git a/assignment5/Question1/Movable.java b/assignment5/Question1/Movable.java new file mode 100644 index 0000000..8db52d5 --- /dev/null +++ b/assignment5/Question1/Movable.java @@ -0,0 +1,9 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is a moveable interface. + */ + +public interface Movable { + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed; +} diff --git a/assignment5/Question1/NotEnoughSpeed.java b/assignment5/Question1/NotEnoughSpeed.java new file mode 100644 index 0000000..b864e1f --- /dev/null +++ b/assignment5/Question1/NotEnoughSpeed.java @@ -0,0 +1,15 @@ +/* + * The NotEnoughSpeed class is used to test the speed of the program. + */ +public class NotEnoughSpeed extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = NotEnoughSpeed.class.getName().hashCode(); + + public NotEnoughSpeed() { + super(); + } + + public NotEnoughSpeed(String message) { + super(message); + } +} diff --git a/assignment5/Question1/Start.java b/assignment5/Question1/Start.java new file mode 100644 index 0000000..a21b424 --- /dev/null +++ b/assignment5/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Vehicle.testVehicle(); + } +} diff --git a/assignment5/Question1/Vehicle.java b/assignment5/Question1/Vehicle.java new file mode 100644 index 0000000..0d77748 --- /dev/null +++ b/assignment5/Question1/Vehicle.java @@ -0,0 +1,180 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is the abstract Vehicle class. + */ + +public abstract class Vehicle implements Movable { + private int speedLimit; + private int speed; + + /** + * Constructor. + * + * @param speedLimit the speed limit of the vehicle + * @param speed the speed of the vehicle + * @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than speed limit. + */ + public Vehicle(int speedLimit, int speed) throws BadSpeedSetting { + if (speedLimit < 0 || speed < 0) { + throw new BadSpeedSetting("Speed cannot be negative!"); + } + if (speed > speedLimit) { + throw new BadSpeedSetting("Speed cannot be greater than speed limit!"); + } + this.speedLimit = speedLimit; + this.speed = speed; + } + + /** + * Getter of speed. + * + * @return the speed of the vehicle + */ + public int getSpeed() { + return speed; + } + + /** + * Accelerate the vehicle. + * + * @param amount the amount of acceleration. + * @throws ExceedSpeedLimit if the speed is greater than speed limit after + * increased. + * @thorws NotEnoughSpeed if the speed is less than 0 after decreased. + * @return the new speed of the vehicle + */ + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed { + int newSpeed = speed + amount; + if (newSpeed > speedLimit) { + throw new ExceedSpeedLimit( + String.format("Current speed is %d. Accelerate %d will exceed the speed limit!", + speed, amount)); + } + if (newSpeed < 0) { + throw new NotEnoughSpeed( + String.format("Current speed is %d, not enough speed to decelerate!", speed)); + } + speed = newSpeed; + return speed; + } + + /** + * Whether the vehicle can fly or not. + * + * @return whether the vehicle can fly. + */ + public abstract boolean canFly(); + + /** + * Test. + */ + public static void testVehicle() { + try { + Vehicle car; + try { + car = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + car = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + car = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return false; + } + }; + System.out.println(car.getSpeed() == 50); + System.out.println(car.canFly() == false); + System.out.println(car.accelerate(10) == 60); + System.out.println(car.accelerate(10) == 70); + System.out.println(car.accelerate(10) == 80); + System.out.println(car.accelerate(10) == 90); + System.out.println(car.accelerate(10) == 100); + try { + System.out.println(car.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(car.accelerate(-50) == 50); + System.out.println(car.accelerate(-50) == 0); + try { + System.out.println(car.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Vehicle plane; + try { + plane = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + plane = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + plane = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return true; + } + }; + System.out.println(plane.getSpeed() == 50); + System.out.println(plane.canFly() == true); + System.out.println(plane.accelerate(10) == 60); + System.out.println(plane.accelerate(10) == 70); + System.out.println(plane.accelerate(10) == 80); + System.out.println(plane.accelerate(10) == 90); + System.out.println(plane.accelerate(10) == 100); + try { + System.out.println(plane.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(plane.accelerate(-50) == 50); + System.out.println(plane.accelerate(-50) == 0); + try { + System.out.println(plane.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } 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: Uncatched exception!"); + } + } +} diff --git a/assignment5/Question2/BadSpeedSetting.java b/assignment5/Question2/BadSpeedSetting.java new file mode 100644 index 0000000..2a08413 --- /dev/null +++ b/assignment5/Question2/BadSpeedSetting.java @@ -0,0 +1,15 @@ +/* + * The BadSpeedSetting class is used to test the speed of the program. + */ +public class BadSpeedSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadSpeedSetting.class.getName().hashCode(); + + public BadSpeedSetting() { + super(); + } + + public BadSpeedSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question2/Car.java b/assignment5/Question2/Car.java new file mode 100644 index 0000000..7017ab2 --- /dev/null +++ b/assignment5/Question2/Car.java @@ -0,0 +1,130 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Car class + */ + +public class Car extends Vehicle { + + /** + * Car can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Constructor. + * + * @param speedLimit The speed limit of the car. + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than the speed limit. + */ + public Car(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Constructor. Default speed limit is 120. + * + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than + * the speed limit 120. + */ + public Car(int speed) throws BadSpeedSetting { + this(120, speed); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car c1; + try { + c1 = new Car(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, 121); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + c1 = new Car(120, 100); + System.out.println(c1.getSpeed() == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 110); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 120); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c1.accelerate(-20) == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 80); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 60); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 40); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 20); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 0); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(-20); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Car c2 = new Car(100); + System.out.println(c2.getSpeed() == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 110); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 120); + System.out.println(c2.canFly() == false); + try { + c2.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c2.accelerate(-20) == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 80); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 60); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 40); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 20); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 0); + System.out.println(c2.canFly() == false); + try { + c2.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 uncatched exception: " + e.getMessage()); + } + } +} diff --git a/assignment5/Question2/ExceedSpeedLimit.java b/assignment5/Question2/ExceedSpeedLimit.java new file mode 100644 index 0000000..ce76da8 --- /dev/null +++ b/assignment5/Question2/ExceedSpeedLimit.java @@ -0,0 +1,15 @@ +/* + * The ExceedSpeedLimit class is used to check if the speed limit is exceeded + */ +public class ExceedSpeedLimit extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = ExceedSpeedLimit.class.getName().hashCode(); + + public ExceedSpeedLimit() { + super(); + } + + public ExceedSpeedLimit(String message) { + super(message); + } +} diff --git a/assignment5/Question2/Movable.java b/assignment5/Question2/Movable.java new file mode 100644 index 0000000..8db52d5 --- /dev/null +++ b/assignment5/Question2/Movable.java @@ -0,0 +1,9 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is a moveable interface. + */ + +public interface Movable { + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed; +} diff --git a/assignment5/Question2/NotEnoughSpeed.java b/assignment5/Question2/NotEnoughSpeed.java new file mode 100644 index 0000000..b864e1f --- /dev/null +++ b/assignment5/Question2/NotEnoughSpeed.java @@ -0,0 +1,15 @@ +/* + * The NotEnoughSpeed class is used to test the speed of the program. + */ +public class NotEnoughSpeed extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = NotEnoughSpeed.class.getName().hashCode(); + + public NotEnoughSpeed() { + super(); + } + + public NotEnoughSpeed(String message) { + super(message); + } +} diff --git a/assignment5/Question2/Start.java b/assignment5/Question2/Start.java new file mode 100644 index 0000000..bb07732 --- /dev/null +++ b/assignment5/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Vehicle.testVehicle(); + Car.testCar(); + } +} diff --git a/assignment5/Question2/Vehicle.java b/assignment5/Question2/Vehicle.java new file mode 100644 index 0000000..0d77748 --- /dev/null +++ b/assignment5/Question2/Vehicle.java @@ -0,0 +1,180 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is the abstract Vehicle class. + */ + +public abstract class Vehicle implements Movable { + private int speedLimit; + private int speed; + + /** + * Constructor. + * + * @param speedLimit the speed limit of the vehicle + * @param speed the speed of the vehicle + * @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than speed limit. + */ + public Vehicle(int speedLimit, int speed) throws BadSpeedSetting { + if (speedLimit < 0 || speed < 0) { + throw new BadSpeedSetting("Speed cannot be negative!"); + } + if (speed > speedLimit) { + throw new BadSpeedSetting("Speed cannot be greater than speed limit!"); + } + this.speedLimit = speedLimit; + this.speed = speed; + } + + /** + * Getter of speed. + * + * @return the speed of the vehicle + */ + public int getSpeed() { + return speed; + } + + /** + * Accelerate the vehicle. + * + * @param amount the amount of acceleration. + * @throws ExceedSpeedLimit if the speed is greater than speed limit after + * increased. + * @thorws NotEnoughSpeed if the speed is less than 0 after decreased. + * @return the new speed of the vehicle + */ + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed { + int newSpeed = speed + amount; + if (newSpeed > speedLimit) { + throw new ExceedSpeedLimit( + String.format("Current speed is %d. Accelerate %d will exceed the speed limit!", + speed, amount)); + } + if (newSpeed < 0) { + throw new NotEnoughSpeed( + String.format("Current speed is %d, not enough speed to decelerate!", speed)); + } + speed = newSpeed; + return speed; + } + + /** + * Whether the vehicle can fly or not. + * + * @return whether the vehicle can fly. + */ + public abstract boolean canFly(); + + /** + * Test. + */ + public static void testVehicle() { + try { + Vehicle car; + try { + car = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + car = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + car = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return false; + } + }; + System.out.println(car.getSpeed() == 50); + System.out.println(car.canFly() == false); + System.out.println(car.accelerate(10) == 60); + System.out.println(car.accelerate(10) == 70); + System.out.println(car.accelerate(10) == 80); + System.out.println(car.accelerate(10) == 90); + System.out.println(car.accelerate(10) == 100); + try { + System.out.println(car.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(car.accelerate(-50) == 50); + System.out.println(car.accelerate(-50) == 0); + try { + System.out.println(car.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Vehicle plane; + try { + plane = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + plane = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + plane = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return true; + } + }; + System.out.println(plane.getSpeed() == 50); + System.out.println(plane.canFly() == true); + System.out.println(plane.accelerate(10) == 60); + System.out.println(plane.accelerate(10) == 70); + System.out.println(plane.accelerate(10) == 80); + System.out.println(plane.accelerate(10) == 90); + System.out.println(plane.accelerate(10) == 100); + try { + System.out.println(plane.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(plane.accelerate(-50) == 50); + System.out.println(plane.accelerate(-50) == 0); + try { + System.out.println(plane.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } 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: Uncatched exception!"); + } + } +} diff --git a/assignment5/Question3/Aircraft.java b/assignment5/Question3/Aircraft.java new file mode 100644 index 0000000..c234253 --- /dev/null +++ b/assignment5/Question3/Aircraft.java @@ -0,0 +1,127 @@ +/* + * 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()); + } + } +} diff --git a/assignment5/Question3/BadAltitudeSetting.java b/assignment5/Question3/BadAltitudeSetting.java new file mode 100644 index 0000000..c2a1506 --- /dev/null +++ b/assignment5/Question3/BadAltitudeSetting.java @@ -0,0 +1,15 @@ +/* + * The BadAltitudeSetting class is used to test the speed of the program. + */ +public class BadAltitudeSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode(); + + public BadAltitudeSetting() { + super(); + } + + public BadAltitudeSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question3/BadSpeedSetting.java b/assignment5/Question3/BadSpeedSetting.java new file mode 100644 index 0000000..2a08413 --- /dev/null +++ b/assignment5/Question3/BadSpeedSetting.java @@ -0,0 +1,15 @@ +/* + * The BadSpeedSetting class is used to test the speed of the program. + */ +public class BadSpeedSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadSpeedSetting.class.getName().hashCode(); + + public BadSpeedSetting() { + super(); + } + + public BadSpeedSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question3/Car.java b/assignment5/Question3/Car.java new file mode 100644 index 0000000..7017ab2 --- /dev/null +++ b/assignment5/Question3/Car.java @@ -0,0 +1,130 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Car class + */ + +public class Car extends Vehicle { + + /** + * Car can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Constructor. + * + * @param speedLimit The speed limit of the car. + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than the speed limit. + */ + public Car(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Constructor. Default speed limit is 120. + * + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than + * the speed limit 120. + */ + public Car(int speed) throws BadSpeedSetting { + this(120, speed); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car c1; + try { + c1 = new Car(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, 121); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + c1 = new Car(120, 100); + System.out.println(c1.getSpeed() == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 110); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 120); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c1.accelerate(-20) == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 80); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 60); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 40); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 20); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 0); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(-20); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Car c2 = new Car(100); + System.out.println(c2.getSpeed() == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 110); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 120); + System.out.println(c2.canFly() == false); + try { + c2.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c2.accelerate(-20) == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 80); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 60); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 40); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 20); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 0); + System.out.println(c2.canFly() == false); + try { + c2.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 uncatched exception: " + e.getMessage()); + } + } +} diff --git a/assignment5/Question3/ExceedSpeedLimit.java b/assignment5/Question3/ExceedSpeedLimit.java new file mode 100644 index 0000000..ce76da8 --- /dev/null +++ b/assignment5/Question3/ExceedSpeedLimit.java @@ -0,0 +1,15 @@ +/* + * The ExceedSpeedLimit class is used to check if the speed limit is exceeded + */ +public class ExceedSpeedLimit extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = ExceedSpeedLimit.class.getName().hashCode(); + + public ExceedSpeedLimit() { + super(); + } + + public ExceedSpeedLimit(String message) { + super(message); + } +} diff --git a/assignment5/Question3/Movable.java b/assignment5/Question3/Movable.java new file mode 100644 index 0000000..8db52d5 --- /dev/null +++ b/assignment5/Question3/Movable.java @@ -0,0 +1,9 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is a moveable interface. + */ + +public interface Movable { + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed; +} diff --git a/assignment5/Question3/NotEnoughSpeed.java b/assignment5/Question3/NotEnoughSpeed.java new file mode 100644 index 0000000..b864e1f --- /dev/null +++ b/assignment5/Question3/NotEnoughSpeed.java @@ -0,0 +1,15 @@ +/* + * The NotEnoughSpeed class is used to test the speed of the program. + */ +public class NotEnoughSpeed extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = NotEnoughSpeed.class.getName().hashCode(); + + public NotEnoughSpeed() { + super(); + } + + public NotEnoughSpeed(String message) { + super(message); + } +} diff --git a/assignment5/Question3/Start.java b/assignment5/Question3/Start.java new file mode 100644 index 0000000..f073de9 --- /dev/null +++ b/assignment5/Question3/Start.java @@ -0,0 +1,7 @@ +public class Start { + public static void main(String[] args) { + Vehicle.testVehicle(); + Car.testCar(); + Aircraft.testAircraft(); + } +} diff --git a/assignment5/Question3/Vehicle.java b/assignment5/Question3/Vehicle.java new file mode 100644 index 0000000..0d77748 --- /dev/null +++ b/assignment5/Question3/Vehicle.java @@ -0,0 +1,180 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is the abstract Vehicle class. + */ + +public abstract class Vehicle implements Movable { + private int speedLimit; + private int speed; + + /** + * Constructor. + * + * @param speedLimit the speed limit of the vehicle + * @param speed the speed of the vehicle + * @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than speed limit. + */ + public Vehicle(int speedLimit, int speed) throws BadSpeedSetting { + if (speedLimit < 0 || speed < 0) { + throw new BadSpeedSetting("Speed cannot be negative!"); + } + if (speed > speedLimit) { + throw new BadSpeedSetting("Speed cannot be greater than speed limit!"); + } + this.speedLimit = speedLimit; + this.speed = speed; + } + + /** + * Getter of speed. + * + * @return the speed of the vehicle + */ + public int getSpeed() { + return speed; + } + + /** + * Accelerate the vehicle. + * + * @param amount the amount of acceleration. + * @throws ExceedSpeedLimit if the speed is greater than speed limit after + * increased. + * @thorws NotEnoughSpeed if the speed is less than 0 after decreased. + * @return the new speed of the vehicle + */ + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed { + int newSpeed = speed + amount; + if (newSpeed > speedLimit) { + throw new ExceedSpeedLimit( + String.format("Current speed is %d. Accelerate %d will exceed the speed limit!", + speed, amount)); + } + if (newSpeed < 0) { + throw new NotEnoughSpeed( + String.format("Current speed is %d, not enough speed to decelerate!", speed)); + } + speed = newSpeed; + return speed; + } + + /** + * Whether the vehicle can fly or not. + * + * @return whether the vehicle can fly. + */ + public abstract boolean canFly(); + + /** + * Test. + */ + public static void testVehicle() { + try { + Vehicle car; + try { + car = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + car = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + car = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return false; + } + }; + System.out.println(car.getSpeed() == 50); + System.out.println(car.canFly() == false); + System.out.println(car.accelerate(10) == 60); + System.out.println(car.accelerate(10) == 70); + System.out.println(car.accelerate(10) == 80); + System.out.println(car.accelerate(10) == 90); + System.out.println(car.accelerate(10) == 100); + try { + System.out.println(car.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(car.accelerate(-50) == 50); + System.out.println(car.accelerate(-50) == 0); + try { + System.out.println(car.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Vehicle plane; + try { + plane = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + plane = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + plane = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return true; + } + }; + System.out.println(plane.getSpeed() == 50); + System.out.println(plane.canFly() == true); + System.out.println(plane.accelerate(10) == 60); + System.out.println(plane.accelerate(10) == 70); + System.out.println(plane.accelerate(10) == 80); + System.out.println(plane.accelerate(10) == 90); + System.out.println(plane.accelerate(10) == 100); + try { + System.out.println(plane.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(plane.accelerate(-50) == 50); + System.out.println(plane.accelerate(-50) == 0); + try { + System.out.println(plane.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } 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: Uncatched exception!"); + } + } +} diff --git a/assignment5/Question4/Aircraft.java b/assignment5/Question4/Aircraft.java new file mode 100644 index 0000000..c234253 --- /dev/null +++ b/assignment5/Question4/Aircraft.java @@ -0,0 +1,127 @@ +/* + * 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()); + } + } +} diff --git a/assignment5/Question4/BadAltitudeSetting.java b/assignment5/Question4/BadAltitudeSetting.java new file mode 100644 index 0000000..c2a1506 --- /dev/null +++ b/assignment5/Question4/BadAltitudeSetting.java @@ -0,0 +1,15 @@ +/* + * The BadAltitudeSetting class is used to test the speed of the program. + */ +public class BadAltitudeSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode(); + + public BadAltitudeSetting() { + super(); + } + + public BadAltitudeSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question4/BadSpeedSetting.java b/assignment5/Question4/BadSpeedSetting.java new file mode 100644 index 0000000..2a08413 --- /dev/null +++ b/assignment5/Question4/BadSpeedSetting.java @@ -0,0 +1,15 @@ +/* + * The BadSpeedSetting class is used to test the speed of the program. + */ +public class BadSpeedSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadSpeedSetting.class.getName().hashCode(); + + public BadSpeedSetting() { + super(); + } + + public BadSpeedSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question4/Car.java b/assignment5/Question4/Car.java new file mode 100644 index 0000000..7017ab2 --- /dev/null +++ b/assignment5/Question4/Car.java @@ -0,0 +1,130 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Car class + */ + +public class Car extends Vehicle { + + /** + * Car can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Constructor. + * + * @param speedLimit The speed limit of the car. + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than the speed limit. + */ + public Car(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Constructor. Default speed limit is 120. + * + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than + * the speed limit 120. + */ + public Car(int speed) throws BadSpeedSetting { + this(120, speed); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car c1; + try { + c1 = new Car(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, 121); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + c1 = new Car(120, 100); + System.out.println(c1.getSpeed() == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 110); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 120); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c1.accelerate(-20) == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 80); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 60); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 40); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 20); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 0); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(-20); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Car c2 = new Car(100); + System.out.println(c2.getSpeed() == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 110); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 120); + System.out.println(c2.canFly() == false); + try { + c2.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c2.accelerate(-20) == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 80); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 60); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 40); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 20); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 0); + System.out.println(c2.canFly() == false); + try { + c2.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 uncatched exception: " + e.getMessage()); + } + } +} diff --git a/assignment5/Question4/ExceedSpeedLimit.java b/assignment5/Question4/ExceedSpeedLimit.java new file mode 100644 index 0000000..ce76da8 --- /dev/null +++ b/assignment5/Question4/ExceedSpeedLimit.java @@ -0,0 +1,15 @@ +/* + * The ExceedSpeedLimit class is used to check if the speed limit is exceeded + */ +public class ExceedSpeedLimit extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = ExceedSpeedLimit.class.getName().hashCode(); + + public ExceedSpeedLimit() { + super(); + } + + public ExceedSpeedLimit(String message) { + super(message); + } +} diff --git a/assignment5/Question4/Movable.java b/assignment5/Question4/Movable.java new file mode 100644 index 0000000..4632969 --- /dev/null +++ b/assignment5/Question4/Movable.java @@ -0,0 +1,9 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is a moveable interface. + */ + +public interface Movable { + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed, TrainSpeedChange; +} diff --git a/assignment5/Question4/NotEnoughSpeed.java b/assignment5/Question4/NotEnoughSpeed.java new file mode 100644 index 0000000..b864e1f --- /dev/null +++ b/assignment5/Question4/NotEnoughSpeed.java @@ -0,0 +1,15 @@ +/* + * The NotEnoughSpeed class is used to test the speed of the program. + */ +public class NotEnoughSpeed extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = NotEnoughSpeed.class.getName().hashCode(); + + public NotEnoughSpeed() { + super(); + } + + public NotEnoughSpeed(String message) { + super(message); + } +} diff --git a/assignment5/Question4/Start.java b/assignment5/Question4/Start.java new file mode 100644 index 0000000..8cc7e46 --- /dev/null +++ b/assignment5/Question4/Start.java @@ -0,0 +1,8 @@ +public class Start { + public static void main(String[] args) { + Vehicle.testVehicle(); + Car.testCar(); + Aircraft.testAircraft(); + Train.testTrain(); + } +} diff --git a/assignment5/Question4/Train.java b/assignment5/Question4/Train.java new file mode 100644 index 0000000..38c6319 --- /dev/null +++ b/assignment5/Question4/Train.java @@ -0,0 +1,71 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Train class inherited from Vehicle class + */ + +public class Train extends Vehicle { + /** + * Constructor. + * + * @param speedLimit the speed limit of the train + * @param speed the speed of the train + * @throws BadSpeedSetting + */ + public Train(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Train can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Tain speed can not be changed. + * + * @throws TrainSpeedChange + */ + @Override + public int accelerate(int amount) throws TrainSpeedChange { + throw new TrainSpeedChange("Do not try to accelerate or decelerate the train!"); + } + + /** + * Test. + */ + public static void testTrain() { + try { + Train train; + try { + train = new Train(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + train = new Train(100, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + train = new Train(100, 101); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + train = new Train(100, 50); + System.out.println(train.getSpeed() == 50); + try { + train.accelerate(1); + } catch (TrainSpeedChange e) { + System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!")); + } + } catch (Exception e) { + System.out.println("Error: uncaught exception " + e.getClass() + ": " + e.getMessage()); + } + } +} diff --git a/assignment5/Question4/TrainSpeedChange.java b/assignment5/Question4/TrainSpeedChange.java new file mode 100644 index 0000000..b72bfd6 --- /dev/null +++ b/assignment5/Question4/TrainSpeedChange.java @@ -0,0 +1,15 @@ +/* + * The TrainSpeedChange class is used to test the speed of the program. + */ +public class TrainSpeedChange extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = TrainSpeedChange.class.getName().hashCode(); + + public TrainSpeedChange() { + super(); + } + + public TrainSpeedChange(String message) { + super(message); + } +} diff --git a/assignment5/Question4/Vehicle.java b/assignment5/Question4/Vehicle.java new file mode 100644 index 0000000..838f478 --- /dev/null +++ b/assignment5/Question4/Vehicle.java @@ -0,0 +1,180 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is the abstract Vehicle class. + */ + +public abstract class Vehicle implements Movable { + private int speedLimit; + private int speed; + + /** + * Constructor. + * + * @param speedLimit the speed limit of the vehicle + * @param speed the speed of the vehicle + * @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than speed limit. + */ + public Vehicle(int speedLimit, int speed) throws BadSpeedSetting { + if (speedLimit < 0 || speed < 0) { + throw new BadSpeedSetting("Speed cannot be negative!"); + } + if (speed > speedLimit) { + throw new BadSpeedSetting("Speed cannot be greater than speed limit!"); + } + this.speedLimit = speedLimit; + this.speed = speed; + } + + /** + * Getter of speed. + * + * @return the speed of the vehicle + */ + public int getSpeed() { + return speed; + } + + /** + * Accelerate the vehicle. + * + * @param amount the amount of acceleration. + * @throws ExceedSpeedLimit if the speed is greater than speed limit after + * increased. + * @thorws NotEnoughSpeed if the speed is less than 0 after decreased. + * @return the new speed of the vehicle + */ + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed, TrainSpeedChange { + int newSpeed = speed + amount; + if (newSpeed > speedLimit) { + throw new ExceedSpeedLimit( + String.format("Current speed is %d. Accelerate %d will exceed the speed limit!", + speed, amount)); + } + if (newSpeed < 0) { + throw new NotEnoughSpeed( + String.format("Current speed is %d, not enough speed to decelerate!", speed)); + } + speed = newSpeed; + return speed; + } + + /** + * Whether the vehicle can fly or not. + * + * @return whether the vehicle can fly. + */ + public abstract boolean canFly(); + + /** + * Test. + */ + public static void testVehicle() { + try { + Vehicle car; + try { + car = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + car = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + car = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return false; + } + }; + System.out.println(car.getSpeed() == 50); + System.out.println(car.canFly() == false); + System.out.println(car.accelerate(10) == 60); + System.out.println(car.accelerate(10) == 70); + System.out.println(car.accelerate(10) == 80); + System.out.println(car.accelerate(10) == 90); + System.out.println(car.accelerate(10) == 100); + try { + System.out.println(car.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(car.accelerate(-50) == 50); + System.out.println(car.accelerate(-50) == 0); + try { + System.out.println(car.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Vehicle plane; + try { + plane = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + plane = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + plane = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return true; + } + }; + System.out.println(plane.getSpeed() == 50); + System.out.println(plane.canFly() == true); + System.out.println(plane.accelerate(10) == 60); + System.out.println(plane.accelerate(10) == 70); + System.out.println(plane.accelerate(10) == 80); + System.out.println(plane.accelerate(10) == 90); + System.out.println(plane.accelerate(10) == 100); + try { + System.out.println(plane.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(plane.accelerate(-50) == 50); + System.out.println(plane.accelerate(-50) == 0); + try { + System.out.println(plane.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } 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: Uncatched exception!"); + } + } +} diff --git a/assignment5/Question5/Aircraft.java b/assignment5/Question5/Aircraft.java new file mode 100644 index 0000000..c234253 --- /dev/null +++ b/assignment5/Question5/Aircraft.java @@ -0,0 +1,127 @@ +/* + * 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()); + } + } +} diff --git a/assignment5/Question5/BadAltitudeSetting.java b/assignment5/Question5/BadAltitudeSetting.java new file mode 100644 index 0000000..c2a1506 --- /dev/null +++ b/assignment5/Question5/BadAltitudeSetting.java @@ -0,0 +1,15 @@ +/* + * The BadAltitudeSetting class is used to test the speed of the program. + */ +public class BadAltitudeSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode(); + + public BadAltitudeSetting() { + super(); + } + + public BadAltitudeSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question5/BadSpeedSetting.java b/assignment5/Question5/BadSpeedSetting.java new file mode 100644 index 0000000..2a08413 --- /dev/null +++ b/assignment5/Question5/BadSpeedSetting.java @@ -0,0 +1,15 @@ +/* + * The BadSpeedSetting class is used to test the speed of the program. + */ +public class BadSpeedSetting extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = BadSpeedSetting.class.getName().hashCode(); + + public BadSpeedSetting() { + super(); + } + + public BadSpeedSetting(String message) { + super(message); + } +} diff --git a/assignment5/Question5/Car.java b/assignment5/Question5/Car.java new file mode 100644 index 0000000..7017ab2 --- /dev/null +++ b/assignment5/Question5/Car.java @@ -0,0 +1,130 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Car class + */ + +public class Car extends Vehicle { + + /** + * Car can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Constructor. + * + * @param speedLimit The speed limit of the car. + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than the speed limit. + */ + public Car(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Constructor. Default speed limit is 120. + * + * @param speed the speed of the car. + * @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than + * the speed limit 120. + */ + public Car(int speed) throws BadSpeedSetting { + this(120, speed); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car c1; + try { + c1 = new Car(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + c1 = new Car(120, 121); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + c1 = new Car(120, 100); + System.out.println(c1.getSpeed() == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 110); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(10) == 120); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c1.accelerate(-20) == 100); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 80); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 60); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 40); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 20); + System.out.println(c1.canFly() == false); + System.out.println(c1.accelerate(-20) == 0); + System.out.println(c1.canFly() == false); + try { + c1.accelerate(-20); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Car c2 = new Car(100); + System.out.println(c2.getSpeed() == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 110); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(10) == 120); + System.out.println(c2.canFly() == false); + try { + c2.accelerate(10); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 120. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(c2.accelerate(-20) == 100); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 80); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 60); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 40); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 20); + System.out.println(c2.canFly() == false); + System.out.println(c2.accelerate(-20) == 0); + System.out.println(c2.canFly() == false); + try { + c2.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 uncatched exception: " + e.getMessage()); + } + } +} diff --git a/assignment5/Question5/ExceedSpeedLimit.java b/assignment5/Question5/ExceedSpeedLimit.java new file mode 100644 index 0000000..ce76da8 --- /dev/null +++ b/assignment5/Question5/ExceedSpeedLimit.java @@ -0,0 +1,15 @@ +/* + * The ExceedSpeedLimit class is used to check if the speed limit is exceeded + */ +public class ExceedSpeedLimit extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = ExceedSpeedLimit.class.getName().hashCode(); + + public ExceedSpeedLimit() { + super(); + } + + public ExceedSpeedLimit(String message) { + super(message); + } +} diff --git a/assignment5/Question5/ManyVehicles.java b/assignment5/Question5/ManyVehicles.java new file mode 100644 index 0000000..316e9d6 --- /dev/null +++ b/assignment5/Question5/ManyVehicles.java @@ -0,0 +1,58 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: ManyVehicles class manipulate many vehicles + */ + +import java.util.ArrayList; + +public class ManyVehicles { + private ArrayList vehicles; + + /** + * Constructor. + */ + public ManyVehicles() { + // initialise instance variables + vehicles = new ArrayList(); + } + + /** + * Add any kind of vehicle to the arraylist. + * + * @param v the vehicle to be added + */ + public void addVehicle(Vehicle v) { + vehicles.add(v); + } + + /** + * Calculate the avergae speed of all vehicles. + * + * @return the average speed of all vehicles + */ + public double averageSpeed() { + double totalSpeed = 0; + for (Vehicle v : vehicles) { + totalSpeed += v.getSpeed(); + } + return totalSpeed / vehicles.size(); + } + + /** + * Test. + */ + public static void testManyVehicles() { + try { + ManyVehicles mv = new ManyVehicles(); + mv.addVehicle(new Car(100, 100)); + mv.addVehicle(new Car(90)); + mv.addVehicle(new Aircraft(100, 70, 3900)); + mv.addVehicle(new Train(100, 60)); + + System.out.println(mv.averageSpeed() == 80.0); + } catch (Exception e) { + System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage()); + } + } +} diff --git a/assignment5/Question5/Movable.java b/assignment5/Question5/Movable.java new file mode 100644 index 0000000..4632969 --- /dev/null +++ b/assignment5/Question5/Movable.java @@ -0,0 +1,9 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is a moveable interface. + */ + +public interface Movable { + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed, TrainSpeedChange; +} diff --git a/assignment5/Question5/NotEnoughSpeed.java b/assignment5/Question5/NotEnoughSpeed.java new file mode 100644 index 0000000..b864e1f --- /dev/null +++ b/assignment5/Question5/NotEnoughSpeed.java @@ -0,0 +1,15 @@ +/* + * The NotEnoughSpeed class is used to test the speed of the program. + */ +public class NotEnoughSpeed extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = NotEnoughSpeed.class.getName().hashCode(); + + public NotEnoughSpeed() { + super(); + } + + public NotEnoughSpeed(String message) { + super(message); + } +} diff --git a/assignment5/Question5/Start.java b/assignment5/Question5/Start.java new file mode 100644 index 0000000..bc803e0 --- /dev/null +++ b/assignment5/Question5/Start.java @@ -0,0 +1,9 @@ +public class Start { + public static void main(String[] args) { + Vehicle.testVehicle(); + Car.testCar(); + Aircraft.testAircraft(); + Train.testTrain(); + ManyVehicles.testManyVehicles(); + } +} diff --git a/assignment5/Question5/Train.java b/assignment5/Question5/Train.java new file mode 100644 index 0000000..38c6319 --- /dev/null +++ b/assignment5/Question5/Train.java @@ -0,0 +1,71 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: Train class inherited from Vehicle class + */ + +public class Train extends Vehicle { + /** + * Constructor. + * + * @param speedLimit the speed limit of the train + * @param speed the speed of the train + * @throws BadSpeedSetting + */ + public Train(int speedLimit, int speed) throws BadSpeedSetting { + super(speedLimit, speed); + } + + /** + * Train can not fly. + * + * @return false + */ + @Override + public boolean canFly() { + return false; + } + + /** + * Tain speed can not be changed. + * + * @throws TrainSpeedChange + */ + @Override + public int accelerate(int amount) throws TrainSpeedChange { + throw new TrainSpeedChange("Do not try to accelerate or decelerate the train!"); + } + + /** + * Test. + */ + public static void testTrain() { + try { + Train train; + try { + train = new Train(-1, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + train = new Train(100, -1); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + train = new Train(100, 101); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + train = new Train(100, 50); + System.out.println(train.getSpeed() == 50); + try { + train.accelerate(1); + } catch (TrainSpeedChange e) { + System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!")); + } + } catch (Exception e) { + System.out.println("Error: uncaught exception " + e.getClass() + ": " + e.getMessage()); + } + } +} diff --git a/assignment5/Question5/TrainSpeedChange.java b/assignment5/Question5/TrainSpeedChange.java new file mode 100644 index 0000000..b72bfd6 --- /dev/null +++ b/assignment5/Question5/TrainSpeedChange.java @@ -0,0 +1,15 @@ +/* + * The TrainSpeedChange class is used to test the speed of the program. + */ +public class TrainSpeedChange extends Exception { + // generate serial ID by class name + private static final long serialVersionUID = TrainSpeedChange.class.getName().hashCode(); + + public TrainSpeedChange() { + super(); + } + + public TrainSpeedChange(String message) { + super(message); + } +} diff --git a/assignment5/Question5/Vehicle.java b/assignment5/Question5/Vehicle.java new file mode 100644 index 0000000..838f478 --- /dev/null +++ b/assignment5/Question5/Vehicle.java @@ -0,0 +1,180 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-24 + * Description: This is the abstract Vehicle class. + */ + +public abstract class Vehicle implements Movable { + private int speedLimit; + private int speed; + + /** + * Constructor. + * + * @param speedLimit the speed limit of the vehicle + * @param speed the speed of the vehicle + * @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed + * is greater than speed limit. + */ + public Vehicle(int speedLimit, int speed) throws BadSpeedSetting { + if (speedLimit < 0 || speed < 0) { + throw new BadSpeedSetting("Speed cannot be negative!"); + } + if (speed > speedLimit) { + throw new BadSpeedSetting("Speed cannot be greater than speed limit!"); + } + this.speedLimit = speedLimit; + this.speed = speed; + } + + /** + * Getter of speed. + * + * @return the speed of the vehicle + */ + public int getSpeed() { + return speed; + } + + /** + * Accelerate the vehicle. + * + * @param amount the amount of acceleration. + * @throws ExceedSpeedLimit if the speed is greater than speed limit after + * increased. + * @thorws NotEnoughSpeed if the speed is less than 0 after decreased. + * @return the new speed of the vehicle + */ + public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed, TrainSpeedChange { + int newSpeed = speed + amount; + if (newSpeed > speedLimit) { + throw new ExceedSpeedLimit( + String.format("Current speed is %d. Accelerate %d will exceed the speed limit!", + speed, amount)); + } + if (newSpeed < 0) { + throw new NotEnoughSpeed( + String.format("Current speed is %d, not enough speed to decelerate!", speed)); + } + speed = newSpeed; + return speed; + } + + /** + * Whether the vehicle can fly or not. + * + * @return whether the vehicle can fly. + */ + public abstract boolean canFly(); + + /** + * Test. + */ + public static void testVehicle() { + try { + Vehicle car; + try { + car = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + car = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return false; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + car = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return false; + } + }; + System.out.println(car.getSpeed() == 50); + System.out.println(car.canFly() == false); + System.out.println(car.accelerate(10) == 60); + System.out.println(car.accelerate(10) == 70); + System.out.println(car.accelerate(10) == 80); + System.out.println(car.accelerate(10) == 90); + System.out.println(car.accelerate(10) == 100); + try { + System.out.println(car.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(car.accelerate(-50) == 50); + System.out.println(car.accelerate(-50) == 0); + try { + System.out.println(car.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage() + .equals("Current speed is 0, not enough speed to decelerate!")); + } + + Vehicle plane; + try { + plane = new Vehicle(-10, -20) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be negative!")); + } + try { + plane = new Vehicle(0, 50) { + @Override + public boolean canFly() { + return true; + } + }; + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + plane = new Vehicle(100, 50) { + @Override + public boolean canFly() { + return true; + } + }; + System.out.println(plane.getSpeed() == 50); + System.out.println(plane.canFly() == true); + System.out.println(plane.accelerate(10) == 60); + System.out.println(plane.accelerate(10) == 70); + System.out.println(plane.accelerate(10) == 80); + System.out.println(plane.accelerate(10) == 90); + System.out.println(plane.accelerate(10) == 100); + try { + System.out.println(plane.accelerate(10) == 110); + System.out.println("Error: ExceedSpeedLimit should be thrown!"); + } catch (ExceedSpeedLimit e) { + System.out.println(e.getMessage().equals( + "Current speed is 100. Accelerate 10 will exceed the speed limit!")); + } + System.out.println(plane.accelerate(-50) == 50); + System.out.println(plane.accelerate(-50) == 0); + try { + System.out.println(plane.accelerate(-50) == -50); + System.out.println("Error: NotEnoughSpeed should be thrown!"); + } 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: Uncatched exception!"); + } + } +}