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