Compare commits

...

41 Commits

Author SHA1 Message Date
5dfbf0b8ae a6 2022-05-25 13:14:19 +08:00
a9f7967976 lab 13 2022-05-25 13:14:19 +08:00
d0416b02bd lab12 2022-05-25 13:14:16 +08:00
1357f94bfb Merge branch 'finalproject' 2022-05-25 13:11:26 +08:00
88a587102f final project q12 giveup 2022-05-24 23:54:02 +08:00
252d0c67bb final project q11 2022-05-24 23:51:13 +08:00
621b5c355d final project q10 2022-05-24 23:46:41 +08:00
16eacad6a1 final project q9 2022-05-24 23:41:21 +08:00
3dc6689804 final project q8 2022-05-24 23:35:04 +08:00
d3cf0c7a7c final project q7 2022-05-24 23:19:13 +08:00
07ed06c619 final project q6 2022-05-24 23:07:43 +08:00
707750c9c8 final project q5 2022-05-24 22:40:38 +08:00
e2bd453330 final project q4 2022-05-24 22:32:29 +08:00
1c6ea20c0a final project q3 2022-05-24 22:23:20 +08:00
6347507ede final project q2 2022-05-24 22:08:20 +08:00
7adb69e3a1 rename to test 2022-05-24 22:07:48 +08:00
fa9befb1b1 format code 2022-05-24 17:56:17 +08:00
243a44f7ed add lab11 2022-05-11 23:36:36 +08:00
855bc8cc3c Fix: a5 testcode close #11 2022-05-02 01:09:47 +08:00
053113577f lab10 2022-05-01 23:24:00 +08:00
0b354c7561 Merge pull request 'a5-test' (#12) from a5-test into master
Reviewed-on: https://yongyuancv.cn/git/heimoshuiyu/oop/pulls/12
2022-04-28 12:07:32 +08:00
4909a9d59f Upload files to 'assignment5/Question5' 2022-04-28 12:06:28 +08:00
b4632d9392 Upload files to 'assignment5/Question4' 2022-04-28 12:06:18 +08:00
59807092f1 Upload files to 'assignment5/Question3' 2022-04-28 12:06:09 +08:00
e94a64535b Upload files to 'assignment5/Question2' 2022-04-28 12:05:55 +08:00
a94d0e9d87 Upload files to 'assignment5/Question1' 2022-04-28 12:05:25 +08:00
c1acb5b92e finalproject q1 2022-04-25 10:20:31 +08:00
e20c82d559 Add: a5 close #9 2022-04-24 13:24:47 +08:00
4a4c378b87 Add: a4 test cases 2022-04-24 11:06:08 +08:00
0ced996a6e lab 9 close #7 2022-04-23 00:21:49 +08:00
941b3d26d4 a4 close #6 2022-04-21 15:43:38 +08:00
8bb5c66d2c Add: a3 auto grader 2022-04-17 12:04:58 +08:00
f4dc400ae2 Merge branch 'lab8' 2022-04-17 12:03:03 +08:00
cea6585033 lab8 finished 2022-04-17 11:41:41 +08:00
fdb4e8296d lab8 q1 2022-04-14 19:03:03 +08:00
76086d0550 A3 q2 fix close a3 bug fix 2022-04-13 12:02:29 +08:00
63c1182007 Merge branch 'a3' close #3 Assignment 3 2022-04-11 14:07:35 +08:00
f35a39f138 a3 question2 2022-04-11 14:06:40 +08:00
8fe0ec8e12 a3 move to Question1 2022-04-11 13:59:00 +08:00
8ae38c9f2b a3 part 2 2022-04-11 13:58:33 +08:00
72874e2cc3 a3 part 1 2022-04-11 11:02:56 +08:00
403 changed files with 17775 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: European rabbit
*/
public class EuropeanRabbit extends Rabbit {
/**
* Constructor.
*
* @param name The name of the Rabbit.
* @param weight The weight of the Rabbit.
*/
public EuropeanRabbit(String name, double weight) {
super(name, weight);
}
/**
* Constructor. With default weight 2.0
*
* @param name The name of the Rabbit.
*/
public EuropeanRabbit(String name) {
this(name, 2.0);
}
/**
* Test.
*/
public static void testEuropeanRabbit() {
// test first constructor
Rabbit rabbit = new EuropeanRabbit("Bugs Bunny");
System.out.println(rabbit.getName() == "Bugs Bunny");
System.out.println(rabbit.getWeight() == 2.0);
System.out.println(rabbit.isCookable() == true);
// test second constructor
Rabbit rabbit2 = new EuropeanRabbit("Daffy Duck", 3.0);
System.out.println(rabbit2.getName() == "Daffy Duck");
System.out.println(rabbit2.getWeight() == 3.0);
System.out.println(rabbit2.isCookable() == true);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: European rabbit
*/
public class FrankTheRabbit extends Rabbit {
/**
* Constructor.
*
* With name "Frank" and weight of 100.0
*/
public FrankTheRabbit() {
super("Frank", 100.0);
}
/**
* Frank rabbit can not be cooked.
*
* @return false
*/
@Override
public boolean isCookable() {
return false;
}
/**
* Test.
*/
public static void testFrankTheRabbit() {
FrankTheRabbit frank = new FrankTheRabbit();
System.out.println(frank.getName() == "Frank");
System.out.println(frank.getWeight() == 100.0);
System.out.println(frank.isCookable() == false);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Human class
*/
public class Human extends Mammal {
/**
* Constructor.
*
* The Constructor take no argument. All human are named "Alice"
*/
public Human() {
super("Alice");
}
/**
* Human cannot be cooked.
*
* @return false
*/
@Override
public boolean isCookable() {
return false;
}
/**
* Test.
*/
public static void testHuman() {
Human human = new Human();
System.out.println(human.getName() == "Alice");
System.out.println(human.isCookable() == false);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Lapin Saute Chasseur
*/
public class LapinSauteChasseur extends EuropeanRabbit {
/**
* Constructor.
*
* With name "Delicious" and has a weight of 0.5
*/
public LapinSauteChasseur() {
super("Delicious", 0.5);
}
/**
* Test.
*/
public static void testLapinSauteChasseur() {
LapinSauteChasseur rabbit = new LapinSauteChasseur();
System.out.println(rabbit.getName() == "Delicious");
System.out.println(rabbit.getWeight() == 0.5);
System.out.println(rabbit.isCookable() == true);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Mammal class
*/
public class Mammal {
/**
* Name of the mammal
*/
private String name;
/**
* Constructor
*
* @param name
* name of the mammal
*/
public Mammal(String name) {
this.name = name;
}
/**
* Get the name of the mammal
*
* @return name of the mammal
*/
public String getName() {
return name;
}
/**
* Whether the mammal is can be cooked.
* Some mammals can be cooked and some mammals cannot be cooked so for safety
* reason the isCookable method just prints a message "Do not cook this!" and
* returns false.
*
* @return false
*/
public boolean isCookable() {
System.out.println("Do not cook this!");
return false;
}
/**
* Test.
*/
public static void testMammal() {
Mammal mammal = new Mammal("Mammal 1");
System.out.println(mammal.getName() == "Mammal 1");
System.out.println(mammal.isCookable() == false);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Rabbit class
*/
public class Rabbit extends Mammal {
/**
* The weight of the rabbit.
*/
private double weight;
/**
* Getter for the weight of the rabbit.
*
* @return the weight of the rabbit
*/
public double getWeight() {
return weight;
}
/**
* Constructor.
*
* @param name
* the name of the rabbit
* @param weight
* the weight of the rabbit
*/
public Rabbit(String name, double weight) {
super(name);
this.weight = weight;
}
/**
* Rabbit can be cooked.
*
* return true
*/
@Override
public boolean isCookable() {
return true;
}
/**
* Test.
*/
public static void testRabbit() {
Rabbit rabbit = new Rabbit("Rabbit", 1.0);
System.out.println(rabbit.getName() == "Rabbit");
System.out.println(rabbit.getWeight() == 1.0);
System.out.println(rabbit.isCookable() == true);
}
}

View File

@@ -0,0 +1,10 @@
public class Start {
public static void main(String[] args) {
Mammal.testMammal();
Human.testHuman();
Rabbit.testRabbit();
EuropeanRabbit.testEuropeanRabbit();
LapinSauteChasseur.testLapinSauteChasseur();
FrankTheRabbit.testFrankTheRabbit();
}
}

View File

@@ -0,0 +1,74 @@
public class StartGrading {
public static void testMammal() {
Mammal m = new Mammal("some name");
System.out.println(m.getName() == "some name");
System.out.println(m.isCookable() == false); // Message printed too.
}
public static void testHuman() {
Human h = new Human();
// getName is inherited from Mammal.
System.out.println(h.getName() == "Alice");
System.out.println(h.isCookable() == false); // No message printed.
}
public static void testRabbit() {
Rabbit r = new Rabbit("Bugs Bunny", 20.0);
// getName is inherited from Mammal.
System.out.println(r.getName() == "Bugs Bunny");
System.out.println(r.getWeight() == 20.0);
System.out.println(r.isCookable() == true);
}
public static void testEuropeanRabbit() {
// Testing the first constructor.
EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0);
// getName is inherited from Mammal.
System.out.println(er1.getName() == "black bunny");
// getWeight is inherited from Rabbit.
System.out.println(er1.getWeight() == 3.0);
// isCookable is inherited from Rabbit.
System.out.println(er1.isCookable() == true);
// Testing the second constructor.
EuropeanRabbit er2 = new EuropeanRabbit("white rabbit");
// getName is inherited from Mammal.
System.out.println(er2.getName() == "white rabbit");
// getWeight is inherited from Rabbit.
System.out.println(er2.getWeight() == 2.0);
// isCookable is inherited from Rabbit.
System.out.println(er2.isCookable() == true);
}
public static void testLapinSauteChasseur() {
LapinSauteChasseur lsc = new LapinSauteChasseur();
// getName is inherited from Mammal.
System.out.println(lsc.getName() == "Delicious");
// getWeight is inherited from Rabbit.
System.out.println(lsc.getWeight() == 0.5);
// isCookable is inherited from Rabbit.
System.out.println(lsc.isCookable() == true);
}
public static void testFrankTheRabbit() {
FrankTheRabbit ftr = new FrankTheRabbit();
// getName is inherited from Mammal.
System.out.println(ftr.getName() == "Frank");
// getWeight is inherited from Rabbit.
System.out.println(ftr.getWeight() == 100.0);
// isCookable is from FrankTheRabbit itself.
System.out.println(ftr.isCookable() == false);
}
public static void main(String[] args) {
testMammal();
testHuman();
testRabbit();
testEuropeanRabbit();
testLapinSauteChasseur();
testFrankTheRabbit();
}
}

View File

@@ -0,0 +1,34 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Mammal class
*/
public class CastIronPot {
private Rabbit rabbit;
/**
* Constructor.
*
* @param rabbit
*/
public CastIronPot(Rabbit rabbit) {
this.rabbit = rabbit;
}
/**
* Getter.
*
* @return rabbit
*/
public Rabbit getRabbit() {
return rabbit;
}
public static void testCastIronPot() {
LapinSauteChasseur lsc1 = new LapinSauteChasseur();
CastIronPot cip = new CastIronPot(lsc1);
LapinSauteChasseur lsc2 = (LapinSauteChasseur) cip.getRabbit();
System.out.println(lsc1 == lsc2);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: European rabbit
*/
public class EuropeanRabbit extends Rabbit {
/**
* Constructor.
*
* @param name The name of the Rabbit.
* @param weight The weight of the Rabbit.
*/
public EuropeanRabbit(String name, double weight) {
super(name, weight);
}
/**
* Constructor. With default weight 2.0
*
* @param name The name of the Rabbit.
*/
public EuropeanRabbit(String name) {
this(name, 2.0);
}
/**
* Test.
*/
public static void testEuropeanRabbit() {
// test first constructor
Rabbit rabbit = new EuropeanRabbit("Bugs Bunny");
System.out.println(rabbit.getName() == "Bugs Bunny");
System.out.println(rabbit.getWeight() == 2.0);
System.out.println(rabbit.isCookable() == true);
// test second constructor
Rabbit rabbit2 = new EuropeanRabbit("Daffy Duck", 3.0);
System.out.println(rabbit2.getName() == "Daffy Duck");
System.out.println(rabbit2.getWeight() == 3.0);
System.out.println(rabbit2.isCookable() == true);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: European rabbit
*/
public class FrankTheRabbit extends Rabbit {
/**
* Constructor.
*
* With name "Frank" and weight of 100.0
*/
public FrankTheRabbit() {
super("Frank", 100.0);
}
/**
* Frank rabbit can not be cooked.
*
* @return false
*/
@Override
public boolean isCookable() {
return false;
}
/**
* Test.
*/
public static void testFrankTheRabbit() {
FrankTheRabbit frank = new FrankTheRabbit();
System.out.println(frank.getName() == "Frank");
System.out.println(frank.getWeight() == 100.0);
System.out.println(frank.isCookable() == false);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Human class
*/
public class Human extends Mammal {
/**
* Constructor.
*
* The Constructor take no argument. All human are named "Alice"
*/
public Human() {
super("Alice");
}
/**
* Human cannot be cooked.
*
* @return false
*/
@Override
public boolean isCookable() {
return false;
}
/**
* Test.
*/
public static void testHuman() {
Human human = new Human();
System.out.println(human.getName() == "Alice");
System.out.println(human.isCookable() == false);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Lapin Saute Chasseur
*/
public class LapinSauteChasseur extends EuropeanRabbit {
/**
* Constructor.
*
* With name "Delicious" and has a weight of 0.5
*/
public LapinSauteChasseur() {
super("Delicious", 0.5);
}
/**
* Test.
*/
public static void testLapinSauteChasseur() {
LapinSauteChasseur rabbit = new LapinSauteChasseur();
System.out.println(rabbit.getName() == "Delicious");
System.out.println(rabbit.getWeight() == 0.5);
System.out.println(rabbit.isCookable() == true);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Mammal class
*/
public class Mammal {
/**
* Name of the mammal
*/
private String name;
/**
* Constructor
*
* @param name
* name of the mammal
*/
public Mammal(String name) {
this.name = name;
}
/**
* Get the name of the mammal
*
* @return name of the mammal
*/
public String getName() {
return name;
}
/**
* Whether the mammal is can be cooked.
* Some mammals can be cooked and some mammals cannot be cooked so for safety
* reason the isCookable method just prints a message "Do not cook this!" and
* returns false.
*
* @return false
*/
public boolean isCookable() {
System.out.println("Do not cook this!");
return false;
}
/**
* Test.
*/
public static void testMammal() {
Mammal mammal = new Mammal("Mammal 1");
System.out.println(mammal.getName() == "Mammal 1");
System.out.println(mammal.isCookable() == false);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-11
* Description: Rabbit class
*/
public class Rabbit extends Mammal {
/**
* The weight of the rabbit.
*/
private double weight;
/**
* Getter for the weight of the rabbit.
*
* @return the weight of the rabbit
*/
public double getWeight() {
return weight;
}
/**
* Constructor.
*
* @param name
* the name of the rabbit
* @param weight
* the weight of the rabbit
*/
public Rabbit(String name, double weight) {
super(name);
this.weight = weight;
}
/**
* Rabbit can be cooked.
*
* return true
*/
@Override
public boolean isCookable() {
return true;
}
/**
* Test.
*/
public static void testRabbit() {
Rabbit rabbit = new Rabbit("Rabbit", 1.0);
System.out.println(rabbit.getName() == "Rabbit");
System.out.println(rabbit.getWeight() == 1.0);
System.out.println(rabbit.isCookable() == true);
}
}

View File

@@ -0,0 +1,11 @@
public class Start {
public static void main(String[] args) {
Mammal.testMammal();
Human.testHuman();
Rabbit.testRabbit();
EuropeanRabbit.testEuropeanRabbit();
LapinSauteChasseur.testLapinSauteChasseur();
FrankTheRabbit.testFrankTheRabbit();
CastIronPot.testCastIronPot();
}
}

View File

@@ -0,0 +1,90 @@
public class StartGrading {
public static void testMammal() {
Mammal m = new Mammal("some name");
System.out.println(m.getName() == "some name");
System.out.println(m.isCookable() == false); // Message printed too.
}
public static void testHuman() {
Human h = new Human();
// getName is inherited from Mammal.
System.out.println(h.getName() == "Alice");
System.out.println(h.isCookable() == false); // No message printed.
}
public static void testRabbit() {
Rabbit r = new Rabbit("Bugs Bunny", 20.0);
// getName is inherited from Mammal.
System.out.println(r.getName() == "Bugs Bunny");
System.out.println(r.getWeight() == 20.0);
System.out.println(r.isCookable() == true);
}
public static void testEuropeanRabbit() {
// Testing the first constructor.
EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0);
// getName is inherited from Mammal.
System.out.println(er1.getName() == "black bunny");
// getWeight is inherited from Rabbit.
System.out.println(er1.getWeight() == 3.0);
// isCookable is inherited from Rabbit.
System.out.println(er1.isCookable() == true);
// Testing the second constructor.
EuropeanRabbit er2 = new EuropeanRabbit("white rabbit");
// getName is inherited from Mammal.
System.out.println(er2.getName() == "white rabbit");
// getWeight is inherited from Rabbit.
System.out.println(er2.getWeight() == 2.0);
// isCookable is inherited from Rabbit.
System.out.println(er2.isCookable() == true);
}
public static void testLapinSauteChasseur() {
LapinSauteChasseur lsc = new LapinSauteChasseur();
// getName is inherited from Mammal.
System.out.println(lsc.getName() == "Delicious");
// getWeight is inherited from Rabbit.
System.out.println(lsc.getWeight() == 0.5);
// isCookable is inherited from Rabbit.
System.out.println(lsc.isCookable() == true);
}
public static void testFrankTheRabbit() {
FrankTheRabbit ftr = new FrankTheRabbit();
// getName is inherited from Mammal.
System.out.println(ftr.getName() == "Frank");
// getWeight is inherited from Rabbit.
System.out.println(ftr.getWeight() == 100.0);
// isCookable is from FrankTheRabbit itself.
System.out.println(ftr.isCookable() == false);
}
public static void testCastIronPot() {
// In the testCastIronPot method, create a lapin saute chasseur called lsc1...
LapinSauteChasseur lsc1 = new LapinSauteChasseur();
// then create a cast iron pot with this lapin saute chasseur in it.
CastIronPot cip = new CastIronPot(lsc1); // Implicit upcast from LapinSauteChasseur to Rabbit.
// Then get the lapin saute chasseur from the cast iron pot...
Rabbit r = cip.getRabbit();
// and store it into a local variable called lsc2 of type LapinSauteChasseur.
LapinSauteChasseur lsc2 = (LapinSauteChasseur)r; // Downcast mandatory!
// Use the == operator to check that lsc1 and lsc2 are the same lapin Saute Chasseur.
System.out.println(lsc1 == lsc2);
}
public static void main(String[] args) {
testMammal();
testHuman();
testRabbit();
testEuropeanRabbit();
testLapinSauteChasseur();
testFrankTheRabbit();
testCastIronPot();
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Course preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Course preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Course getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,14 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
}
}

View File

@@ -0,0 +1,12 @@
public class StartGrading {
public static void testCourse()
{
// Since we cannot create any Course object,
//the testCourse method of the Course class must be empty.
}
public static void main(String[] args)
{
testCourse();
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Course preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Course preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Course getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorRequired class which is used to store the major and required courses.
*/
public class MajorRequired extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
* and cannot be null.
*/
public MajorRequired(String code, String title, MajorRequired preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is required for all students.
*
* @return true
*/
@Override
public boolean isRequired() {
return true;
}
/**
* Test.
*/
public static void testMajorRequired() {
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
System.out.println(mr1.getCode() == null);
System.out.println(mr1.getTitle() == null);
System.out.println(mr1.getPreRequisite() == null);
System.out.println(mr1.isRequired() == true);
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
System.out.println(mr2.getCode() == "COMP3013");
System.out.println(mr2.getTitle() == "Database Management System");
System.out.println(mr2.getPreRequisite() == mr1);
System.out.println(mr2.isRequired() == false);
}
}

View File

@@ -0,0 +1,20 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*
* - Q: What kinds of tests can you write inside the testMajorRequired method?
*
* - A: You can first create a MajorRequired object with null preRequisite and
* do the test as normal.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
MajorRequired.testMajorRequired();
}
}

View File

@@ -0,0 +1,17 @@
public class StartGrading {
public static void testCourse()
{
// Since we cannot create any Course object,
//the testCourse method of the Course class must be empty.
}
public static void testMajorRequired()
{
//Since we cannot create any MajorRequired object,
//the testMajorRequired method of the Course class must be empty.
}
public static void main(String[] args)
{
testCourse();
testMajorRequired();
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Course preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Course preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Course getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorElective class which is elective for students.
*/
public class MajorElective extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorElective
* and cannot be null.
*/
public MajorElective(String code, String title, MajorElective preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is elective for all students.
*
* @return false
*/
public boolean isRequired() {
return false;
}
/**
* Test.
*/
public static void testMajorElective() {
MajorElective pre = new MajorElective("CSC108", "Intro to Programming", null);
System.out.println(pre.getCode() == null);
System.out.println(pre.getTitle() == null);
System.out.println(pre.getPreRequisite() == null);
System.out.println(pre.isRequired() == false);
MajorElective pre2 = new MajorElective("CSC108", "Intro to Programming", pre);
System.out.println(pre2.getCode() == "CSC108");
System.out.println(pre2.getTitle() == "Intro to Programming");
System.out.println(pre2.getPreRequisite() == pre);
System.out.println(pre2.isRequired() == false);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorRequired class which is used to store the major and required courses.
*/
public class MajorRequired extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
* and cannot be null.
*/
public MajorRequired(String code, String title, MajorRequired preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is required for all students.
*
* @return true
*/
@Override
public boolean isRequired() {
return true;
}
/**
* Test.
*/
public static void testMajorRequired() {
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
System.out.println(mr1.getCode() == null);
System.out.println(mr1.getTitle() == null);
System.out.println(mr1.getPreRequisite() == null);
System.out.println(mr1.isRequired() == true);
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
System.out.println(mr2.getCode() == "COMP3013");
System.out.println(mr2.getTitle() == "Database Management System");
System.out.println(mr2.getPreRequisite() == mr1);
System.out.println(mr2.isRequired() == true);
}
}

View File

@@ -0,0 +1,26 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*
* - Q: What kinds of tests can you write inside the testMajorRequired method?
*
* - A: You can first create a MajorRequired object with null preRequisite and
* do the test as normal.
*
* - Q: What kinds of tests can you write inside the testMajorElective method?
*
* - A: You can first create a MajorElective object with null preRequisite and
* do the test as normal.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
MajorRequired.testMajorRequired();
MajorElective.testMajorElective();
}
}

View File

@@ -0,0 +1,26 @@
public class StartGrading {
public static void testCourse()
{
// Since we cannot create any Course object,
//the testCourse method of the Course class must be empty.
}
public static void testMajorRequired()
{
//Since we cannot create any MajorRequired object,
//the testMajorRequired method of the Course class must be empty.
}
public static void testMajorElective()
{
//Since we cannot create any MajorElective object,
//the testMajorElective method of the Course class must be empty.
}
public static void main(String[] args)
{
testCourse();
testMajorElective();
testMajorRequired();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Base Class.
*/
public class Base {
private String code;
private String title;
/**
* Constructor
*
* @param code The code of the course
* @param title The title of the course
*/
public Base(String code, String title) {
this.code = code;
this.title = title;
}
/**
* Getter of code
*
* @return The code of the course
*/
public String getCode() {
return code;
}
/**
* Getter of title
*
* @return The title of the course
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite course. It will return itself.
*
* @return this
*/
public Base getPreRequisite() {
return this;
}
/**
* Test.
*/
public static void testBase() {
Base base = new Base("COMP3013", "Database Management System");
System.out.println(base.getCode() == "COMP3013");
System.out.println(base.getTitle() == "Database Management System");
System.out.println(base.getPreRequisite() == base);
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Course preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Course preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Course getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorElective class which is elective for students.
*/
public class MajorElective extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorElective
* and cannot be null.
*/
public MajorElective(String code, String title, MajorElective preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is elective for all students.
*
* @return false
*/
public boolean isRequired() {
return false;
}
/**
* Test.
*/
public static void testMajorElective() {
MajorElective pre = new MajorElective("CSC108", "Intro to Programming", null);
System.out.println(pre.getCode() == null);
System.out.println(pre.getTitle() == null);
System.out.println(pre.getPreRequisite() == null);
System.out.println(pre.isRequired() == false);
MajorElective pre2 = new MajorElective("CSC108", "Intro to Programming", pre);
System.out.println(pre2.getCode() == "CSC108");
System.out.println(pre2.getTitle() == "Intro to Programming");
System.out.println(pre2.getPreRequisite() == pre);
System.out.println(pre2.isRequired() == false);
}
}

View File

@@ -0,0 +1,46 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorRequired class which is used to store the major and required courses.
*/
public class MajorRequired extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
* and cannot be null.
*/
public MajorRequired(String code, String title, MajorRequired preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is required for all students.
*
* @return true
*/
@Override
public boolean isRequired() {
return true;
}
/**
* Test.
*/
public static void testMajorRequired() {
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
System.out.println(mr1.getCode() == null);
System.out.println(mr1.getTitle() == null);
System.out.println(mr1.getPreRequisite() == null);
System.out.println(mr1.isRequired() == true);
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
System.out.println(mr2.getCode() == "COMP3013");
System.out.println(mr2.getTitle() == "Database Management System");
System.out.println(mr2.getPreRequisite() == mr1);
System.out.println(mr2.isRequired() == true);
}
}

View File

@@ -0,0 +1,27 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*
* - Q: What kinds of tests can you write inside the testMajorRequired method?
*
* - A: You can first create a MajorRequired object with null preRequisite and
* do the test as normal.
*
* - Q: What kinds of tests can you write inside the testMajorElective method?
*
* - A: You can first create a MajorElective object with null preRequisite and
* do the test as normal.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
MajorRequired.testMajorRequired();
MajorElective.testMajorElective();
Base.testBase();
}
}

View File

@@ -0,0 +1,38 @@
public class StartGrading {
public static void testCourse()
{
// Since we cannot create any Course object,
//the testCourse method of the Course class must be empty.
}
public static void testMajorRequired()
{
//Since we cannot create any MajorRequired object,
//the testMajorRequired method of the Course class must be empty.
}
public static void testMajorElective()
{
//Since we cannot create any MajorElective object,
//the testMajorElective method of the Course class must be empty.
}
public static void testBase()
{
Base b = new Base("DS1001", "EntryCourse");
System.out.println(b.getCode() == "DS1001");
System.out.println(b.getTitle() == "EntryCourse");
System.out.println(b.getPreRequisite() == b);
}
public static void main(String[] args)
{
testCourse();
testMajorElective();
testMajorRequired();
testBase();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Base Class.
*/
public class Base implements Learnable {
private String code;
private String title;
/**
* Constructor
*
* @param code The code of the course
* @param title The title of the course
*/
public Base(String code, String title) {
this.code = code;
this.title = title;
}
/**
* Getter of code
*
* @return The code of the course
*/
public String getCode() {
return code;
}
/**
* Getter of title
*
* @return The title of the course
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite course. It will return itself.
*
* @return this
*/
public Learnable getPreRequisite() {
return this;
}
/**
* Test.
*/
public static void testBase() {
Base base = new Base("COMP3013", "Database Management System");
System.out.println(base.getCode() == "COMP3013");
System.out.println(base.getTitle() == "Database Management System");
System.out.println(base.getPreRequisite() == base);
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course implements Learnable {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Learnable preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Learnable preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Learnable getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,13 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Learnable interface.
*/
public interface Learnable {
public String getCode();
public String getTitle();
public Learnable getPreRequisite();
}

View File

@@ -0,0 +1,56 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorElective class which is elective for students.
*/
public class MajorElective extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorElective
* and cannot be null.
*/
public MajorElective(String code, String title, Learnable preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is elective for all students.
*
* @return false
*/
public boolean isRequired() {
return false;
}
/**
* Test.
*/
public static void testMajorElective() {
MajorElective pre = new MajorElective("CSC108", "Intro to Programming", null);
System.out.println(pre.getCode() == null);
System.out.println(pre.getTitle() == null);
System.out.println(pre.getPreRequisite() == null);
System.out.println(pre.isRequired() == false);
MajorElective pre2 = new MajorElective("CSC108", "Intro to Programming", pre);
System.out.println(pre2.getCode() == "CSC108");
System.out.println(pre2.getTitle() == "Intro to Programming");
System.out.println(pre2.getPreRequisite() == pre);
System.out.println(pre2.isRequired() == false);
Base base = new Base("COMP4023", "Software Engineering");
System.out.println(base.getCode() == "COMP4023");
System.out.println(base.getTitle() == "Software Engineering");
System.out.println(base.getPreRequisite() == base);
MajorElective me = new MajorElective("CSC123", "Big Data", base);
System.out.println(me.getCode() == "CSC123");
System.out.println(me.getTitle() == "Big Data");
System.out.println(me.getPreRequisite() == base);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorRequired class which is used to store the major and required courses.
*/
public class MajorRequired extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
* and cannot be null.
*/
public MajorRequired(String code, String title, Learnable preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is required for all students.
*
* @return true
*/
@Override
public boolean isRequired() {
return true;
}
/**
* Test.
*/
public static void testMajorRequired() {
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
System.out.println(mr1.getCode() == null);
System.out.println(mr1.getTitle() == null);
System.out.println(mr1.getPreRequisite() == null);
System.out.println(mr1.isRequired() == true);
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
System.out.println(mr2.getCode() == "COMP3013");
System.out.println(mr2.getTitle() == "Database Management System");
System.out.println(mr2.getPreRequisite() == mr1);
System.out.println(mr2.isRequired() == true);
Base base = new Base("COMP4023", "Software Engineering");
System.out.println(base.getCode() == "COMP4023");
System.out.println(base.getTitle() == "Software Engineering");
System.out.println(base.getPreRequisite() == base);
MajorRequired mr3 = new MajorRequired("COMP4101", "Quality Assurance", base);
System.out.println(mr3.getCode() == "COMP4101");
System.out.println(mr3.getTitle() == "Quality Assurance");
System.out.println(mr3.getPreRequisite() == base);
System.out.println(mr3.isRequired() == true);
}
}

View File

@@ -0,0 +1,27 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*
* - Q: What kinds of tests can you write inside the testMajorRequired method?
*
* - A: You can first create a MajorRequired object with null preRequisite and
* do the test as normal.
*
* - Q: What kinds of tests can you write inside the testMajorElective method?
*
* - A: You can first create a MajorElective object with null preRequisite and
* do the test as normal.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
MajorRequired.testMajorRequired();
MajorElective.testMajorElective();
Base.testBase();
}
}

View File

@@ -0,0 +1,64 @@
public class StartGrading {
public static void testCourse()
{ //Since we cannot create any Course object,
//the testAnimal method of the Course class must be empty.
}
public static void testMajorRequired()
{
//create a Base object
Base b = new Base("DS1001","EntryCourse");
//create a first MajorRequired object with the base course as pre-requisite
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
//create a second MajorRequired object with the first major required course as pre-requisite
MajorRequired mr2 = new MajorRequired("DS300X","Data Mining",mr1);
//test mr1
System.out.println(mr1.getCode()=="DS200X");
System.out.println(mr1.getTitle()=="OOP");
System.out.println(mr1.getPreRequisite() == b);
//test mr2
System.out.println(mr2.getCode()=="DS300X");
System.out.println(mr2.getTitle()=="Data Mining");
System.out.println(mr2.getPreRequisite() == mr1);
}
public static void testMajorElective()
{
//create a Base object
Base b = new Base("DS1001","EntryCourse");
//create a first MajorRequired object with the base course as pre-requisite
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
//create a MajorElective object with the major required course as pre-requisite
MajorElective me1 = new MajorElective("DS300X","Data Mining",mr1);
//test mr1
System.out.println(mr1.getCode()=="DS200X");
System.out.println(mr1.getTitle()=="OOP");
System.out.println(mr1.getPreRequisite() == b);
//test mr2
System.out.println(me1.getCode()=="DS300X");
System.out.println(me1.getTitle()=="Data Mining");
System.out.println(me1.getPreRequisite() == mr1);
}
public static void testBase()
{
Base b = new Base("DS1001", "EntryCourse");
System.out.println(b.getCode() == "DS1001");
System.out.println(b.getTitle() == "EntryCourse");
//System.out.println(b.isRequired() == true);
System.out.println(b.getPreRequisite() == b);
}
public static void main(String[] args)
{
testCourse();
testBase();
testMajorRequired();
testMajorElective();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Base Class.
*/
public class Base implements Learnable {
private String code;
private String title;
/**
* Constructor
*
* @param code The code of the course
* @param title The title of the course
*/
public Base(String code, String title) {
this.code = code;
this.title = title;
}
/**
* Getter of code
*
* @return The code of the course
*/
public String getCode() {
return code;
}
/**
* Getter of title
*
* @return The title of the course
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite course. It will return itself.
*
* @return this
*/
public Learnable getPreRequisite() {
return this;
}
/**
* Test.
*/
public static void testBase() {
Base base = new Base("COMP3013", "Database Management System");
System.out.println(base.getCode() == "COMP3013");
System.out.println(base.getTitle() == "Database Management System");
System.out.println(base.getPreRequisite() == base);
}
}

View File

@@ -0,0 +1,113 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Course class.
*/
public abstract class Course implements Learnable {
/**
* The code of the course.
*/
private String code;
/**
* The title of the course.
*/
private String title;
/**
* The pre-requisite of the course.
*
* A course always has only one pre-requisite course and the pre-requisite
* course cannot be null.
*
* @see Course
*/
private Learnable preRequisite;
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. If the pre-requisite
* course is null, the constructor will print a message of
* "pre-requisite course cannot be null!" and return
* directly.
*/
public Course(String code, String title, Learnable preRequisite) {
if (preRequisite == null) {
System.out.println("pre-requisite course cannot be null!");
return;
}
this.code = code;
this.title = title;
this.preRequisite = preRequisite;
}
/**
* Get the code of the course.
*
* @return The code of the course.
*/
public String getCode() {
return code;
}
/**
* Get the title of the course.
*
* @return The title of the course.
*/
public String getTitle() {
return title;
}
/**
* Get the pre-requisite of the course.
*
* @return The pre-requisite of the course.
*/
public Learnable getPreRequisite() {
return preRequisite;
}
/**
* Indicate whether a course is required or not.
*
* One course could either be a required course or a elective course. Some
* courses are required, while the others are not required.
*
* @return True if the course is required, false if the course is not required.
*/
public abstract boolean isRequired();
/**
* Test.
*/
public static void testCourse() {
Course mr = new Course("CS101", "Intro to Java", null) {
@Override
public boolean isRequired() {
return true;
}
};
System.out.println(mr.getCode() == null);
System.out.println(mr.getTitle() == null);
System.out.println(mr.getPreRequisite() == null);
System.out.println(mr.isRequired() == true);
Course me = new Course("CS102", "Intro to Python", mr) {
@Override
public boolean isRequired() {
return false;
}
};
System.out.println(me.getCode() == "CS102");
System.out.println(me.getTitle() == "Intro to Python");
System.out.println(me.getPreRequisite() == mr);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,13 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the Learnable interface.
*/
public interface Learnable {
public String getCode();
public String getTitle();
public Learnable getPreRequisite();
}

View File

@@ -0,0 +1,56 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorElective class which is elective for students.
*/
public class MajorElective extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorElective
* and cannot be null.
*/
public MajorElective(String code, String title, Learnable preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is elective for all students.
*
* @return false
*/
public boolean isRequired() {
return false;
}
/**
* Test.
*/
public static void testMajorElective() {
MajorElective pre = new MajorElective("CSC108", "Intro to Programming", null);
System.out.println(pre.getCode() == null);
System.out.println(pre.getTitle() == null);
System.out.println(pre.getPreRequisite() == null);
System.out.println(pre.isRequired() == false);
MajorElective pre2 = new MajorElective("CSC108", "Intro to Programming", pre);
System.out.println(pre2.getCode() == "CSC108");
System.out.println(pre2.getTitle() == "Intro to Programming");
System.out.println(pre2.getPreRequisite() == pre);
System.out.println(pre2.isRequired() == false);
Base base = new Base("COMP4023", "Software Engineering");
System.out.println(base.getCode() == "COMP4023");
System.out.println(base.getTitle() == "Software Engineering");
System.out.println(base.getPreRequisite() == base);
MajorElective me = new MajorElective("CSC123", "Big Data", base);
System.out.println(me.getCode() == "CSC123");
System.out.println(me.getTitle() == "Big Data");
System.out.println(me.getPreRequisite() == base);
System.out.println(me.isRequired() == false);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the MajorRequired class which is used to store the major and required courses.
*/
public class MajorRequired extends Course {
/**
* Constructor.
*
* @param code The code of the course.
* @param title The title of the course.
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
* and cannot be null.
*/
public MajorRequired(String code, String title, Learnable preRequisite) {
super(code, title, preRequisite);
}
/**
* MajorRequired course is required for all students.
*
* @return true
*/
@Override
public boolean isRequired() {
return true;
}
/**
* Test.
*/
public static void testMajorRequired() {
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
System.out.println(mr1.getCode() == null);
System.out.println(mr1.getTitle() == null);
System.out.println(mr1.getPreRequisite() == null);
System.out.println(mr1.isRequired() == true);
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
System.out.println(mr2.getCode() == "COMP3013");
System.out.println(mr2.getTitle() == "Database Management System");
System.out.println(mr2.getPreRequisite() == mr1);
System.out.println(mr2.isRequired() == true);
Base base = new Base("COMP4023", "Software Engineering");
System.out.println(base.getCode() == "COMP4023");
System.out.println(base.getTitle() == "Software Engineering");
System.out.println(base.getPreRequisite() == base);
MajorRequired mr3 = new MajorRequired("COMP4101", "Quality Assurance", base);
System.out.println(mr3.getCode() == "COMP4101");
System.out.println(mr3.getTitle() == "Quality Assurance");
System.out.println(mr3.getPreRequisite() == base);
System.out.println(mr3.isRequired() == true);
}
}

View File

@@ -0,0 +1,51 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-18
* Description: This is the ManyCourses class.
*/
import java.util.ArrayList;
public class ManyCourses {
private ArrayList<Learnable> courses;
/**
* Constructor.
*/
public ManyCourses() {
courses = new ArrayList<Learnable>();
}
/**
* Add a course to the list.
*
* @param course
*/
public void addCourse(Learnable c) {
courses.add(c);
}
/**
* Prints on the screen the course code and title.
*/
public void listCourses() {
for (Learnable c : courses) {
System.out.println(
String.format("%s, %s", c.getCode(), c.getTitle()));
}
}
/**
* Test.
*/
public static void testManyCourses() {
Base b = new Base("DS1001", "EntryCourse");
MajorRequired mr1 = new MajorRequired("DS200X", "OOP", b);
MajorElective me1 = new MajorElective("DS300X", "Data Mining", mr1);
ManyCourses mc = new ManyCourses();
mc.addCourse(b);
mc.addCourse(mr1);
mc.addCourse(me1);
mc.listCourses();
}
}

View File

@@ -0,0 +1,28 @@
/**
* Answer to the questions.
*
* - Q: What kinds of tests can you write inside the testCourse method?
*
* - A: You can inherit the Couse class to annonymous class and override the
* isRequired() method.
*
* - Q: What kinds of tests can you write inside the testMajorRequired method?
*
* - A: You can first create a MajorRequired object with null preRequisite and
* do the test as normal.
*
* - Q: What kinds of tests can you write inside the testMajorElective method?
*
* - A: You can first create a MajorElective object with null preRequisite and
* do the test as normal.
*/
public class Start {
public static void main(String[] args) {
Course.testCourse();
MajorRequired.testMajorRequired();
MajorElective.testMajorElective();
Base.testBase();
ManyCourses.testManyCourses();
}
}

View File

@@ -0,0 +1,76 @@
public class StartGrading {
public static void testCourse()
{ //Since we cannot create any Course object,
//the testAnimal method of the Course class must be empty.
}
public static void testMajorRequired()
{
//create a Base object
Base b = new Base("DS1001","EntryCourse");
//create a first MajorRequired object with the base course as pre-requisite
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
//create a second MajorRequired object with the first major required course as pre-requisite
MajorRequired mr2 = new MajorRequired("DS300X","Data Mining",mr1);
//test mr1
System.out.println(mr1.getCode()=="DS200X");
System.out.println(mr1.getTitle()=="OOP");
System.out.println(mr1.getPreRequisite() == b);
//test mr2
System.out.println(mr2.getCode()=="DS300X");
System.out.println(mr2.getTitle()=="Data Mining");
System.out.println(mr2.getPreRequisite() == mr1);
}
public static void testMajorElective()
{
//create a Base object
Base b = new Base("DS1001","EntryCourse");
//create a first MajorRequired object with the base course as pre-requisite
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
//create a MajorElective object with the major required course as pre-requisite
MajorElective me1 = new MajorElective("DS300X","Data Mining",mr1);
//test mr1
System.out.println(mr1.getCode()=="DS200X");
System.out.println(mr1.getTitle()=="OOP");
System.out.println(mr1.getPreRequisite() == b);
//test mr2
System.out.println(me1.getCode()=="DS300X");
System.out.println(me1.getTitle()=="Data Mining");
System.out.println(me1.getPreRequisite() == mr1);
}
public static void testBase()
{
Base b = new Base("DS1001", "EntryCourse");
System.out.println(b.getCode() == "DS1001");
System.out.println(b.getTitle() == "EntryCourse");
//System.out.println(b.isRequired() == true);
System.out.println(b.getPreRequisite() == b);
}
public static void testManyCourses()
{
Base b = new Base("DS1001","EntryCourse");
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
MajorElective me1 = new MajorElective("DS300X","Data Mining",mr1);
ManyCourses mc = new ManyCourses();
mc.addCourse(b);
mc.addCourse(mr1);
mc.addCourse(me1);
mc.listCourses();
}
public static void main(String[] args)
{
testCourse();
testBase();
testMajorRequired();
testMajorElective();
testManyCourses();
}
}

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,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,5 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
}
}

View File

@@ -0,0 +1,10 @@
//Q1
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 main(String[] args) {
Vehicle.testVehicle();
}
}

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

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,58 @@
//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();
}
}

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

View File

@@ -0,0 +1,127 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Aircraft.java inherited from Vehicle class
*/
public class Aircraft extends Vehicle {
/**
* The altitude at which the aircraft is flying.
*/
private int altitude;
/**
* Constructor.
*
* @param speedLimit the speed limit of the aircraft
* @param speed the speed of the aircraft
* @param altitude the altitude of the aircraft
* @throws BadSpeedSetting if the speed limit is negative
* @throws BadAltitudeSetting if the altitude is negative
*/
public Aircraft(int speedLimit, int speed, int altitude) throws BadSpeedSetting, BadAltitudeSetting {
super(speedLimit, speed);
if (altitude < 0) {
throw new BadAltitudeSetting("Altitude cannot be negative!");
}
this.altitude = altitude;
}
/**
* Getter of altitude.
*
* @return the altitude of the aircraft
*/
public int getAltitude() {
return altitude;
}
/**
* Aircraft can fly.
*
* @return true
*/
@Override
public boolean canFly() {
return true;
}
/**
* Test.
*/
public static void testAircraft() {
try {
Aircraft aircraft;
try {
aircraft = new Aircraft(-10, 10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, -10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, 10, -10);
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
aircraft = new Aircraft(100, 50, 3900);
System.out.println(aircraft.getAltitude() == 3900);
System.out.println(aircraft.getSpeed() == 50);
System.out.println(aircraft.canFly() == true);
// acceleration
System.out.println(aircraft.accelerate(10) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 70);
System.out.println(aircraft.getSpeed() == 70);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 90);
System.out.println(aircraft.getSpeed() == 90);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 100);
System.out.println(aircraft.getSpeed() == 100);
System.out.println(aircraft.canFly() == true);
try {
aircraft.accelerate(10);
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
// deceleration
System.out.println(aircraft.accelerate(-20) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 40);
System.out.println(aircraft.getSpeed() == 40);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 20);
System.out.println(aircraft.getSpeed() == 20);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 0);
System.out.println(aircraft.getSpeed() == 0);
System.out.println(aircraft.canFly() == true);
try {
aircraft.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 uncaught exception: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* The BadAltitudeSetting class is used to test the speed of the program.
*/
public class BadAltitudeSetting extends Exception {
// generate serial ID by class name
private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode();
public BadAltitudeSetting() {
super();
}
public BadAltitudeSetting(String message) {
super(message);
}
}

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,7 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
}
}

View File

@@ -0,0 +1,125 @@
//Q3
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 testAircraft() {
// test case for constructors
try {
// Exception
Aircraft a1 = new Aircraft(70, 80, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a2 = new Aircraft(70, -10, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a3 = new Aircraft(700, 200, -10);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
// Test case for accelerate, canFly, getSpeed and getAltitude methods
try {
Aircraft a4 = new Aircraft(700, 200, 100);
// test the getAltitude method
System.out.println(a4.getAltitude() == 100);
// test the canFly method
System.out.println(a4.canFly() == true);
a4.accelerate(100);
System.out.println(a4.getSpeed() == 300);
// test the accelerate with positive amount
a4.accelerate(-200);
System.out.println(a4.getSpeed() == 100);
// test the accelerate with negative amount
// exception
a4.accelerate(650);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
System.out.println(
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a5 = new Aircraft(700, 200, 100);
// exception
a5.accelerate(-300);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting 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 " + 200 + ", not enough speed to decelerate!"));
}
}
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
}
}

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

View File

@@ -0,0 +1,127 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Aircraft.java inherited from Vehicle class
*/
public class Aircraft extends Vehicle {
/**
* The altitude at which the aircraft is flying.
*/
private int altitude;
/**
* Constructor.
*
* @param speedLimit the speed limit of the aircraft
* @param speed the speed of the aircraft
* @param altitude the altitude of the aircraft
* @throws BadSpeedSetting if the speed limit is negative
* @throws BadAltitudeSetting if the altitude is negative
*/
public Aircraft(int speedLimit, int speed, int altitude) throws BadSpeedSetting, BadAltitudeSetting {
super(speedLimit, speed);
if (altitude < 0) {
throw new BadAltitudeSetting("Altitude cannot be negative!");
}
this.altitude = altitude;
}
/**
* Getter of altitude.
*
* @return the altitude of the aircraft
*/
public int getAltitude() {
return altitude;
}
/**
* Aircraft can fly.
*
* @return true
*/
@Override
public boolean canFly() {
return true;
}
/**
* Test.
*/
public static void testAircraft() {
try {
Aircraft aircraft;
try {
aircraft = new Aircraft(-10, 10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, -10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, 10, -10);
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
aircraft = new Aircraft(100, 50, 3900);
System.out.println(aircraft.getAltitude() == 3900);
System.out.println(aircraft.getSpeed() == 50);
System.out.println(aircraft.canFly() == true);
// acceleration
System.out.println(aircraft.accelerate(10) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 70);
System.out.println(aircraft.getSpeed() == 70);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 90);
System.out.println(aircraft.getSpeed() == 90);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 100);
System.out.println(aircraft.getSpeed() == 100);
System.out.println(aircraft.canFly() == true);
try {
aircraft.accelerate(10);
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
// deceleration
System.out.println(aircraft.accelerate(-20) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 40);
System.out.println(aircraft.getSpeed() == 40);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 20);
System.out.println(aircraft.getSpeed() == 20);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 0);
System.out.println(aircraft.getSpeed() == 0);
System.out.println(aircraft.canFly() == true);
try {
aircraft.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 uncaught exception: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* The BadAltitudeSetting class is used to test the speed of the program.
*/
public class BadAltitudeSetting extends Exception {
// generate serial ID by class name
private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode();
public BadAltitudeSetting() {
super();
}
public BadAltitudeSetting(String message) {
super(message);
}
}

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, TrainSpeedChange;
}

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,8 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
}
}

View File

@@ -0,0 +1,164 @@
//Q4
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!");
} catch (TrainSpeedChange 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!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
public static void testAircraft() {
// test case for constructors
try {
// Exception
Aircraft a1 = new Aircraft(70, 80, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a2 = new Aircraft(70, -10, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a3 = new Aircraft(700, 200, -10);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
// Test case for accelerate, canFly, getSpeed and getAltitude methods
try {
Aircraft a4 = new Aircraft(700, 200, 100);
// test the getAltitude method
System.out.println(a4.getAltitude() == 100);
// test the canFly method
System.out.println(a4.canFly() == true);
a4.accelerate(100);
System.out.println(a4.getSpeed() == 300);
// test the accelerate with positive amount
a4.accelerate(-200);
System.out.println(a4.getSpeed() == 100);
// test the accelerate with negative amount
// exception
a4.accelerate(650);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
System.out.println(
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a5 = new Aircraft(700, 200, 100);
// exception
a5.accelerate(-300);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting 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 " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
public static void testTrain() {
// test case for constructors
try {
// exception
Train t1 = new Train(350, 400);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
Train t2 = new Train(350, -100);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
try {
Train t3 = new Train(350, 300);
// test the canFly method
System.out.println(t3.canFly() == false);
// test the getSpeed
System.out.println(t3.getSpeed() == 300);
// exception
t3.accelerate(50);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
}
}
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
}
}

View File

@@ -0,0 +1,71 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Train class inherited from Vehicle class
*/
public class Train extends Vehicle {
/**
* Constructor.
*
* @param speedLimit the speed limit of the train
* @param speed the speed of the train
* @throws BadSpeedSetting
*/
public Train(int speedLimit, int speed) throws BadSpeedSetting {
super(speedLimit, speed);
}
/**
* Train can not fly.
*
* @return false
*/
@Override
public boolean canFly() {
return false;
}
/**
* Tain speed can not be changed.
*
* @throws TrainSpeedChange
*/
@Override
public int accelerate(int amount) throws TrainSpeedChange {
throw new TrainSpeedChange("Do not try to accelerate or decelerate the train!");
}
/**
* Test.
*/
public static void testTrain() {
try {
Train train;
try {
train = new Train(-1, -1);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
train = new Train(100, -1);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
train = new Train(100, 101);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
train = new Train(100, 50);
System.out.println(train.getSpeed() == 50);
try {
train.accelerate(1);
} catch (TrainSpeedChange e) {
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
}
} catch (Exception e) {
System.out.println("Error: uncaught exception " + e.getClass() + ": " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* The TrainSpeedChange class is used to test the speed of the program.
*/
public class TrainSpeedChange extends Exception {
// generate serial ID by class name
private static final long serialVersionUID = TrainSpeedChange.class.getName().hashCode();
public TrainSpeedChange() {
super();
}
public TrainSpeedChange(String message) {
super(message);
}
}

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

View File

@@ -0,0 +1,127 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Aircraft.java inherited from Vehicle class
*/
public class Aircraft extends Vehicle {
/**
* The altitude at which the aircraft is flying.
*/
private int altitude;
/**
* Constructor.
*
* @param speedLimit the speed limit of the aircraft
* @param speed the speed of the aircraft
* @param altitude the altitude of the aircraft
* @throws BadSpeedSetting if the speed limit is negative
* @throws BadAltitudeSetting if the altitude is negative
*/
public Aircraft(int speedLimit, int speed, int altitude) throws BadSpeedSetting, BadAltitudeSetting {
super(speedLimit, speed);
if (altitude < 0) {
throw new BadAltitudeSetting("Altitude cannot be negative!");
}
this.altitude = altitude;
}
/**
* Getter of altitude.
*
* @return the altitude of the aircraft
*/
public int getAltitude() {
return altitude;
}
/**
* Aircraft can fly.
*
* @return true
*/
@Override
public boolean canFly() {
return true;
}
/**
* Test.
*/
public static void testAircraft() {
try {
Aircraft aircraft;
try {
aircraft = new Aircraft(-10, 10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, -10, 10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
aircraft = new Aircraft(10, 10, -10);
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
aircraft = new Aircraft(100, 50, 3900);
System.out.println(aircraft.getAltitude() == 3900);
System.out.println(aircraft.getSpeed() == 50);
System.out.println(aircraft.canFly() == true);
// acceleration
System.out.println(aircraft.accelerate(10) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 70);
System.out.println(aircraft.getSpeed() == 70);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 90);
System.out.println(aircraft.getSpeed() == 90);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(10) == 100);
System.out.println(aircraft.getSpeed() == 100);
System.out.println(aircraft.canFly() == true);
try {
aircraft.accelerate(10);
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
// deceleration
System.out.println(aircraft.accelerate(-20) == 80);
System.out.println(aircraft.getSpeed() == 80);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 60);
System.out.println(aircraft.getSpeed() == 60);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 40);
System.out.println(aircraft.getSpeed() == 40);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 20);
System.out.println(aircraft.getSpeed() == 20);
System.out.println(aircraft.canFly() == true);
System.out.println(aircraft.accelerate(-20) == 0);
System.out.println(aircraft.getSpeed() == 0);
System.out.println(aircraft.canFly() == true);
try {
aircraft.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 uncaught exception: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* The BadAltitudeSetting class is used to test the speed of the program.
*/
public class BadAltitudeSetting extends Exception {
// generate serial ID by class name
private static final long serialVersionUID = BadAltitudeSetting.class.getName().hashCode();
public BadAltitudeSetting() {
super();
}
public BadAltitudeSetting(String message) {
super(message);
}
}

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,58 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: ManyVehicles class manipulate many vehicles
*/
import java.util.ArrayList;
public class ManyVehicles {
private ArrayList<Vehicle> vehicles;
/**
* Constructor.
*/
public ManyVehicles() {
// initialise instance variables
vehicles = new ArrayList<Vehicle>();
}
/**
* Add any kind of vehicle to the arraylist.
*
* @param v the vehicle to be added
*/
public void addVehicle(Vehicle v) {
vehicles.add(v);
}
/**
* Calculate the avergae speed of all vehicles.
*
* @return the average speed of all vehicles
*/
public double calcAvgSpeed() {
double totalSpeed = 0;
for (Vehicle v : vehicles) {
totalSpeed += v.getSpeed();
}
return totalSpeed / vehicles.size();
}
/**
* Test.
*/
public static void testManyVehicles() {
try {
ManyVehicles mv = new ManyVehicles();
mv.addVehicle(new Car(100, 100));
mv.addVehicle(new Car(90));
mv.addVehicle(new Aircraft(100, 70, 3900));
mv.addVehicle(new Train(100, 60));
System.out.println(mv.calcAvgSpeed() == 80.0);
} catch (Exception e) {
System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage());
}
}
}

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, TrainSpeedChange;
}

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,9 @@
public class Start {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
ManyVehicles.testManyVehicles();
}
}

View File

@@ -0,0 +1,190 @@
//Q5
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!");
} catch (TrainSpeedChange 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!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
public static void testAircraft() {
// test case for constructors
try {
// Exception
Aircraft a1 = new Aircraft(70, 80, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a2 = new Aircraft(70, -10, 1000);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
// exception
Aircraft a3 = new Aircraft(700, 200, -10);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println(e.getMessage().equals("Altitude cannot be negative!"));
}
// Test case for accelerate, canFly, getSpeed and getAltitude methods
try {
Aircraft a4 = new Aircraft(700, 200, 100);
// test the getAltitude method
System.out.println(a4.getAltitude() == 100);
// test the canFly method
System.out.println(a4.canFly() == true);
a4.accelerate(100);
System.out.println(a4.getSpeed() == 300);
// test the accelerate with positive amount
a4.accelerate(-200);
System.out.println(a4.getSpeed() == 100);
// test the accelerate with negative amount
// exception
a4.accelerate(650);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
System.out.println(
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a5 = new Aircraft(700, 200, 100);
// exception
a5.accelerate(-300);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting 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 " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
public static void testTrain() {
// test case for constructors
try {
// exception
Train t1 = new Train(350, 400);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
Train t2 = new Train(350, -100);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
try {
Train t3 = new Train(350, 300);
// test the canFly method
System.out.println(t3.canFly() == false);
// test the getSpeed
System.out.println(t3.getSpeed() == 300);
// exception
t3.accelerate(50);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
}
}
public static void testManyVehicles() {
ManyVehicles mv = new ManyVehicles();
// Add any kind of vehicles to the arraylist.
try {
Train t = new Train(350, 300);
mv.addVehicle(t);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Car c = new Car(100, 80);
mv.addVehicle(c);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a = new Aircraft(700, 200, 100);
mv.addVehicle(a);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (BadAltitudeSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
System.out.println(mv.calcAvgSpeed() == 193);
}
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
Aircraft.testAircraft();
Train.testTrain();
}
}

View File

@@ -0,0 +1,71 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: Train class inherited from Vehicle class
*/
public class Train extends Vehicle {
/**
* Constructor.
*
* @param speedLimit the speed limit of the train
* @param speed the speed of the train
* @throws BadSpeedSetting
*/
public Train(int speedLimit, int speed) throws BadSpeedSetting {
super(speedLimit, speed);
}
/**
* Train can not fly.
*
* @return false
*/
@Override
public boolean canFly() {
return false;
}
/**
* Tain speed can not be changed.
*
* @throws TrainSpeedChange
*/
@Override
public int accelerate(int amount) throws TrainSpeedChange {
throw new TrainSpeedChange("Do not try to accelerate or decelerate the train!");
}
/**
* Test.
*/
public static void testTrain() {
try {
Train train;
try {
train = new Train(-1, -1);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
train = new Train(100, -1);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
train = new Train(100, 101);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
train = new Train(100, 50);
System.out.println(train.getSpeed() == 50);
try {
train.accelerate(1);
} catch (TrainSpeedChange e) {
System.out.println(e.getMessage().equals("Do not try to accelerate or decelerate the train!"));
}
} catch (Exception e) {
System.out.println("Error: uncaught exception " + e.getClass() + ": " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* The TrainSpeedChange class is used to test the speed of the program.
*/
public class TrainSpeedChange extends Exception {
// generate serial ID by class name
private static final long serialVersionUID = TrainSpeedChange.class.getName().hashCode();
public TrainSpeedChange() {
super();
}
public TrainSpeedChange(String message) {
super(message);
}
}

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

Some files were not shown because too many files have changed in this diff Show More