From e94a64535b26274e6f91007b46c375cc97ce1499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E5=A2=A8=E6=B0=B4=E9=B1=BC?= Date: Thu, 28 Apr 2022 12:05:55 +0800 Subject: [PATCH] Upload files to 'assignment5/Question2' --- assignment5/Question2/StartGrading.java | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 assignment5/Question2/StartGrading.java diff --git a/assignment5/Question2/StartGrading.java b/assignment5/Question2/StartGrading.java new file mode 100644 index 0000000..3e27226 --- /dev/null +++ b/assignment5/Question2/StartGrading.java @@ -0,0 +1,60 @@ +//Q2 +public class StartGrading { + public static void testVehicle() { +//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 + Car c1 = new Car(70, 80); + } catch (BadSpeedSetting e) { + System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!")); + } + try { +// 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 + try { + Car c3 = new Car(100, 80); +// 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 + c3.accelerate(-20); + System.out.println(c3.getSpeed() == 70); +// test the accelerate with negative amount + c3.accelerate(35); +// exception + } catch (BadSpeedSetting e) { + System.out.println("BUG! THIS MUST NEVER HAPPEN!"); + } catch (ExceedSpeedLimit e) { + System.out + .println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!")); + } catch (NotEnoughSpeed e) { + System.out.println("BUG! THIS MUST NEVER HAPPEN!"); + } + try { + Car c4 = new Car(100, 80); +// exception + c4.accelerate(-90); + } catch (BadSpeedSetting e) { + System.out.println("BUG! THIS MUST NEVER HAPPEN!"); + } catch (ExceedSpeedLimit e) { + System.out.println("BUG! THIS MUST NEVER HAPPEN!"); + } catch (NotEnoughSpeed e) { + System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!")); + } + } + + public static void main(String[] args) { + Vehicle.testVehicle(); + Car.testCar(); + } +}