Add: a5 close #9
This commit is contained in:
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user