Add: a5 close #9

This commit is contained in:
2022-04-24 13:24:47 +08:00
parent 4a4c378b87
commit e20c82d559
45 changed files with 2381 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,5 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
}
}

View File

@@ -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!");
}
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
}
}

View File

@@ -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!");
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,7 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
}
}

View File

@@ -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!");
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,8 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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!");
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<Vehicle> vehicles;
/**
* Constructor.
*/
public ManyVehicles() {
// initialise instance variables
vehicles = new ArrayList<Vehicle>();
}
/**
* 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());
}
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,9 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
ManyVehicles.testManyVehicles();
}
}

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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!");
}
}
}