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