Add: a5 close #9
This commit is contained in:
15
assignment5/Question1/BadSpeedSetting.java
Normal file
15
assignment5/Question1/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question1/ExceedSpeedLimit.java
Normal file
15
assignment5/Question1/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question1/Movable.java
Normal file
9
assignment5/Question1/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question1/NotEnoughSpeed.java
Normal file
15
assignment5/Question1/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
5
assignment5/Question1/Start.java
Normal file
5
assignment5/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question1/Vehicle.java
Normal file
180
assignment5/Question1/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question2/BadSpeedSetting.java
Normal file
15
assignment5/Question2/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question2/Car.java
Normal file
130
assignment5/Question2/Car.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question2/ExceedSpeedLimit.java
Normal file
15
assignment5/Question2/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question2/Movable.java
Normal file
9
assignment5/Question2/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question2/NotEnoughSpeed.java
Normal file
15
assignment5/Question2/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
6
assignment5/Question2/Start.java
Normal file
6
assignment5/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question2/Vehicle.java
Normal file
180
assignment5/Question2/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question3/Aircraft.java
Normal file
127
assignment5/Question3/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/BadAltitudeSetting.java
Normal file
15
assignment5/Question3/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/BadSpeedSetting.java
Normal file
15
assignment5/Question3/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question3/Car.java
Normal file
130
assignment5/Question3/Car.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/ExceedSpeedLimit.java
Normal file
15
assignment5/Question3/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question3/Movable.java
Normal file
9
assignment5/Question3/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question3/NotEnoughSpeed.java
Normal file
15
assignment5/Question3/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
7
assignment5/Question3/Start.java
Normal file
7
assignment5/Question3/Start.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question3/Vehicle.java
Normal file
180
assignment5/Question3/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question4/Aircraft.java
Normal file
127
assignment5/Question4/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/BadAltitudeSetting.java
Normal file
15
assignment5/Question4/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/BadSpeedSetting.java
Normal file
15
assignment5/Question4/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question4/Car.java
Normal file
130
assignment5/Question4/Car.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/ExceedSpeedLimit.java
Normal file
15
assignment5/Question4/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question4/Movable.java
Normal file
9
assignment5/Question4/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question4/NotEnoughSpeed.java
Normal file
15
assignment5/Question4/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
8
assignment5/Question4/Start.java
Normal file
8
assignment5/Question4/Start.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
Train.testTrain();
|
||||
}
|
||||
}
|
||||
71
assignment5/Question4/Train.java
Normal file
71
assignment5/Question4/Train.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/TrainSpeedChange.java
Normal file
15
assignment5/Question4/TrainSpeedChange.java
Normal 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);
|
||||
}
|
||||
}
|
||||
180
assignment5/Question4/Vehicle.java
Normal file
180
assignment5/Question4/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question5/Aircraft.java
Normal file
127
assignment5/Question5/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/BadAltitudeSetting.java
Normal file
15
assignment5/Question5/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/BadSpeedSetting.java
Normal file
15
assignment5/Question5/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question5/Car.java
Normal file
130
assignment5/Question5/Car.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/ExceedSpeedLimit.java
Normal file
15
assignment5/Question5/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
58
assignment5/Question5/ManyVehicles.java
Normal file
58
assignment5/Question5/ManyVehicles.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
9
assignment5/Question5/Movable.java
Normal file
9
assignment5/Question5/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question5/NotEnoughSpeed.java
Normal file
15
assignment5/Question5/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question5/Start.java
Normal file
9
assignment5/Question5/Start.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
Train.testTrain();
|
||||
ManyVehicles.testManyVehicles();
|
||||
}
|
||||
}
|
||||
71
assignment5/Question5/Train.java
Normal file
71
assignment5/Question5/Train.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/TrainSpeedChange.java
Normal file
15
assignment5/Question5/TrainSpeedChange.java
Normal 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);
|
||||
}
|
||||
}
|
||||
180
assignment5/Question5/Vehicle.java
Normal file
180
assignment5/Question5/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user