Fix: a5 testcode close #11

This commit is contained in:
2022-05-02 01:09:47 +08:00
parent 053113577f
commit 855bc8cc3c
5 changed files with 58 additions and 46 deletions

View File

@@ -4,34 +4,32 @@ public class StartGrading {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything //The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
} }
public static void testCar() { public static void testCar() {
// test case for constructors // test case for constructors
try { try {
// exception // exception
Car c1 = new Car(70, 80); Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} }
try { try {
// exception // exception
Car c2 = new Car(70, -10); Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!")); System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} }
// test case for accelerate, canFly and getSpeed methods // test case for accelerate, canFly and getSpeed methods
try { try {
Car c3 = new Car(100, 80); Car c3 = new Car(100, 80);
// test the canFly method // test the canFly method
System.out.println(c3.canFly() == false); System.out.println(c3.canFly() == false);
c3.accelerate(10); c3.accelerate(10);
System.out.println(c3.getSpeed() == 90); System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount // test the accelerate with positive amount
c3.accelerate(-20); c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70); System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount // test the accelerate with negative amount
c3.accelerate(35); c3.accelerate(35); // exception
// exception
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) { } catch (ExceedSpeedLimit e) {
@@ -42,7 +40,7 @@ public class StartGrading {
} }
try { try {
Car c4 = new Car(100, 80); Car c4 = new Car(100, 80);
// exception // exception
c4.accelerate(-90); c4.accelerate(-90);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");

View File

@@ -7,30 +7,29 @@ public class StartGrading {
public static void testCar() { public static void testCar() {
// test case for constructors // test case for constructors
try { try {
// exception // exception
Car c1 = new Car(70, 80); Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} }
try { try {
// exception // exception
Car c2 = new Car(70, -10); Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!")); System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} }
// test case for accelerate, canFly and getSpeed methods // test case for accelerate, canFly and getSpeed methods
try { try {
Car c3 = new Car(100, 80); Car c3 = new Car(100, 80);
// test the canFly method // test the canFly method
System.out.println(c3.canFly() == false); System.out.println(c3.canFly() == false);
c3.accelerate(10); c3.accelerate(10);
System.out.println(c3.getSpeed() == 90); System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount // test the accelerate with positive amount
c3.accelerate(-20); c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70); System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount // test the accelerate with negative amount
c3.accelerate(35); c3.accelerate(35); // exception
// exception
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) { } catch (ExceedSpeedLimit e) {
@@ -41,7 +40,7 @@ public class StartGrading {
} }
try { try {
Car c4 = new Car(100, 80); Car c4 = new Car(100, 80);
// exception // exception
c4.accelerate(-90); c4.accelerate(-90);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");

View File

@@ -1,3 +1,4 @@
//Q4
public class StartGrading { public class StartGrading {
public static void testVehicle() { public static void testVehicle() {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything //The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
@@ -6,30 +7,29 @@ public class StartGrading {
public static void testCar() { public static void testCar() {
// test case for constructors // test case for constructors
try { try {
// exception // exception
Car c1 = new Car(70, 80); Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} }
try { try {
// exception // exception
Car c2 = new Car(70, -10); Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!")); System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} }
// test case for accelerate, canFly and getSpeed methods // test case for accelerate, canFly and getSpeed methods
try { try {
Car c3 = new Car(100, 80); Car c3 = new Car(100, 80);
// test the canFly method // test the canFly method
System.out.println(c3.canFly() == false); System.out.println(c3.canFly() == false);
c3.accelerate(10); c3.accelerate(10);
System.out.println(c3.getSpeed() == 90); System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount // test the accelerate with positive amount
c3.accelerate(-20); c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70); System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount // test the accelerate with negative amount
c3.accelerate(35); c3.accelerate(35); // exception
// exception
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) { } catch (ExceedSpeedLimit e) {
@@ -37,10 +37,12 @@ public class StartGrading {
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!")); .println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
try { try {
Car c4 = new Car(100, 80); Car c4 = new Car(100, 80);
// exception // exception
c4.accelerate(-90); c4.accelerate(-90);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -48,6 +50,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!")); System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
} }
@@ -101,6 +105,8 @@ public class StartGrading {
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!")); e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
try { try {
Aircraft a5 = new Aircraft(700, 200, 100); Aircraft a5 = new Aircraft(700, 200, 100);
@@ -114,6 +120,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!")); System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
} }
@@ -150,7 +158,7 @@ public class StartGrading {
public static void main(String[] args) { public static void main(String[] args) {
Vehicle.testVehicle(); Vehicle.testVehicle();
Car.testCar(); Car.testCar();
AirCraft.testAirCraft(); Aircraft.testAircraft();
Train.testTrain(); Train.testTrain();
} }
} }

View File

@@ -31,7 +31,7 @@ public class ManyVehicles {
* *
* @return the average speed of all vehicles * @return the average speed of all vehicles
*/ */
public double averageSpeed() { public double calcAvgSpeed() {
double totalSpeed = 0; double totalSpeed = 0;
for (Vehicle v : vehicles) { for (Vehicle v : vehicles) {
totalSpeed += v.getSpeed(); totalSpeed += v.getSpeed();
@@ -50,7 +50,7 @@ public class ManyVehicles {
mv.addVehicle(new Aircraft(100, 70, 3900)); mv.addVehicle(new Aircraft(100, 70, 3900));
mv.addVehicle(new Train(100, 60)); mv.addVehicle(new Train(100, 60));
System.out.println(mv.averageSpeed() == 80.0); System.out.println(mv.calcAvgSpeed() == 80.0);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage()); System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage());
} }

View File

@@ -1,3 +1,4 @@
//Q5
public class StartGrading { public class StartGrading {
public static void testVehicle() { public static void testVehicle() {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything //The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
@@ -6,30 +7,29 @@ public class StartGrading {
public static void testCar() { public static void testCar() {
// test case for constructors // test case for constructors
try { try {
// exception // exception
Car c1 = new Car(70, 80); Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} }
try { try {
// exception // exception
Car c2 = new Car(70, -10); Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!")); System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} }
// test case for accelerate, canFly and getSpeed methods // test case for accelerate, canFly and getSpeed methods
try { try {
Car c3 = new Car(100, 80); Car c3 = new Car(100, 80);
// test the canFly method // test the canFly method
System.out.println(c3.canFly() == false); System.out.println(c3.canFly() == false);
c3.accelerate(10); c3.accelerate(10);
System.out.println(c3.getSpeed() == 90); System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount // test the accelerate with positive amount
c3.accelerate(-20); c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70); System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount // test the accelerate with negative amount
c3.accelerate(35); c3.accelerate(35); // exception
// exception
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) { } catch (ExceedSpeedLimit e) {
@@ -37,10 +37,12 @@ public class StartGrading {
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!")); .println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
try { try {
Car c4 = new Car(100, 80); Car c4 = new Car(100, 80);
// exception // exception
c4.accelerate(-90); c4.accelerate(-90);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -48,6 +50,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!")); System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
} }
@@ -101,6 +105,8 @@ public class StartGrading {
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!")); e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
try { try {
Aircraft a5 = new Aircraft(700, 200, 100); Aircraft a5 = new Aircraft(700, 200, 100);
@@ -114,6 +120,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) { } catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!")); System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
} }
@@ -149,7 +157,7 @@ public class StartGrading {
public static void testManyVehicles() { public static void testManyVehicles() {
ManyVehicles mv = new ManyVehicles(); ManyVehicles mv = new ManyVehicles();
// Add any kind of vehicles to the arraylist. // Add any kind of vehicles to the arraylist.
try { try {
Train t = new Train(350, 300); Train t = new Train(350, 300);
mv.addVehicle(t); mv.addVehicle(t);
@@ -163,7 +171,7 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} }
try { try {
AirCraft a = new AirCraft(700, 200, 100); Aircraft a = new Aircraft(700, 200, 100);
mv.addVehicle(a); mv.addVehicle(a);
} catch (BadSpeedSetting e) { } catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!"); System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -176,8 +184,7 @@ public class StartGrading {
public static void main(String[] args) { public static void main(String[] args) {
Vehicle.testVehicle(); Vehicle.testVehicle();
Car.testCar(); Car.testCar();
AirCraft.testAirCraft(); Aircraft.testAircraft();
Train.testTrain(); Train.testTrain();
ManyVehicles.testVehicles();
} }
} }