Compare commits
41 Commits
5e9fc07694
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
5dfbf0b8ae
|
|||
|
a9f7967976
|
|||
|
d0416b02bd
|
|||
|
1357f94bfb
|
|||
|
88a587102f
|
|||
|
252d0c67bb
|
|||
|
621b5c355d
|
|||
|
16eacad6a1
|
|||
|
3dc6689804
|
|||
|
d3cf0c7a7c
|
|||
|
07ed06c619
|
|||
|
707750c9c8
|
|||
|
e2bd453330
|
|||
|
1c6ea20c0a
|
|||
|
6347507ede
|
|||
|
7adb69e3a1
|
|||
|
fa9befb1b1
|
|||
|
243a44f7ed
|
|||
|
855bc8cc3c
|
|||
|
053113577f
|
|||
| 0b354c7561 | |||
| 4909a9d59f | |||
| b4632d9392 | |||
| 59807092f1 | |||
| e94a64535b | |||
| a94d0e9d87 | |||
|
c1acb5b92e
|
|||
|
e20c82d559
|
|||
|
4a4c378b87
|
|||
|
0ced996a6e
|
|||
|
941b3d26d4
|
|||
|
8bb5c66d2c
|
|||
|
f4dc400ae2
|
|||
|
cea6585033
|
|||
|
fdb4e8296d
|
|||
|
76086d0550
|
|||
|
63c1182007
|
|||
|
f35a39f138
|
|||
|
8fe0ec8e12
|
|||
|
8ae38c9f2b
|
|||
|
72874e2cc3
|
43
assignment3/Question1/EuropeanRabbit.java
Normal file
43
assignment3/Question1/EuropeanRabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
36
assignment3/Question1/FrankTheRabbit.java
Normal file
36
assignment3/Question1/FrankTheRabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
36
assignment3/Question1/Human.java
Normal file
36
assignment3/Question1/Human.java
Normal 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);
|
||||
}
|
||||
}
|
||||
26
assignment3/Question1/LapinSauteChasseur.java
Normal file
26
assignment3/Question1/LapinSauteChasseur.java
Normal 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);
|
||||
}
|
||||
}
|
||||
53
assignment3/Question1/Mammal.java
Normal file
53
assignment3/Question1/Mammal.java
Normal 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);
|
||||
}
|
||||
}
|
||||
54
assignment3/Question1/Rabbit.java
Normal file
54
assignment3/Question1/Rabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
10
assignment3/Question1/Start.java
Normal file
10
assignment3/Question1/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
74
assignment3/Question1/StartGrading.java
Normal file
74
assignment3/Question1/StartGrading.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
34
assignment3/Question2/CastIronPot.java
Normal file
34
assignment3/Question2/CastIronPot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
43
assignment3/Question2/EuropeanRabbit.java
Normal file
43
assignment3/Question2/EuropeanRabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
36
assignment3/Question2/FrankTheRabbit.java
Normal file
36
assignment3/Question2/FrankTheRabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
36
assignment3/Question2/Human.java
Normal file
36
assignment3/Question2/Human.java
Normal 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);
|
||||
}
|
||||
}
|
||||
26
assignment3/Question2/LapinSauteChasseur.java
Normal file
26
assignment3/Question2/LapinSauteChasseur.java
Normal 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);
|
||||
}
|
||||
}
|
||||
53
assignment3/Question2/Mammal.java
Normal file
53
assignment3/Question2/Mammal.java
Normal 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);
|
||||
}
|
||||
}
|
||||
54
assignment3/Question2/Rabbit.java
Normal file
54
assignment3/Question2/Rabbit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
11
assignment3/Question2/Start.java
Normal file
11
assignment3/Question2/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
90
assignment3/Question2/StartGrading.java
Normal file
90
assignment3/Question2/StartGrading.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
113
assignment4/Question1/Course.java
Normal file
113
assignment4/Question1/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
14
assignment4/Question1/Start.java
Normal file
14
assignment4/Question1/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
12
assignment4/Question1/StartGrading.java
Normal file
12
assignment4/Question1/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
113
assignment4/Question2/Course.java
Normal file
113
assignment4/Question2/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
46
assignment4/Question2/MajorRequired.java
Normal file
46
assignment4/Question2/MajorRequired.java
Normal 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);
|
||||
}
|
||||
}
|
||||
20
assignment4/Question2/Start.java
Normal file
20
assignment4/Question2/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
17
assignment4/Question2/StartGrading.java
Normal file
17
assignment4/Question2/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
113
assignment4/Question3/Course.java
Normal file
113
assignment4/Question3/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
45
assignment4/Question3/MajorElective.java
Normal file
45
assignment4/Question3/MajorElective.java
Normal 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);
|
||||
}
|
||||
}
|
||||
46
assignment4/Question3/MajorRequired.java
Normal file
46
assignment4/Question3/MajorRequired.java
Normal 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);
|
||||
}
|
||||
}
|
||||
26
assignment4/Question3/Start.java
Normal file
26
assignment4/Question3/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
26
assignment4/Question3/StartGrading.java
Normal file
26
assignment4/Question3/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
58
assignment4/Question4/Base.java
Normal file
58
assignment4/Question4/Base.java
Normal 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);
|
||||
}
|
||||
}
|
||||
113
assignment4/Question4/Course.java
Normal file
113
assignment4/Question4/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
45
assignment4/Question4/MajorElective.java
Normal file
45
assignment4/Question4/MajorElective.java
Normal 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);
|
||||
}
|
||||
}
|
||||
46
assignment4/Question4/MajorRequired.java
Normal file
46
assignment4/Question4/MajorRequired.java
Normal 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);
|
||||
}
|
||||
}
|
||||
27
assignment4/Question4/Start.java
Normal file
27
assignment4/Question4/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
38
assignment4/Question4/StartGrading.java
Normal file
38
assignment4/Question4/StartGrading.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
58
assignment4/Question5/Base.java
Normal file
58
assignment4/Question5/Base.java
Normal 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);
|
||||
}
|
||||
}
|
||||
113
assignment4/Question5/Course.java
Normal file
113
assignment4/Question5/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
13
assignment4/Question5/Learnable.java
Normal file
13
assignment4/Question5/Learnable.java
Normal 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();
|
||||
}
|
||||
56
assignment4/Question5/MajorElective.java
Normal file
56
assignment4/Question5/MajorElective.java
Normal 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);
|
||||
}
|
||||
}
|
||||
57
assignment4/Question5/MajorRequired.java
Normal file
57
assignment4/Question5/MajorRequired.java
Normal 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);
|
||||
}
|
||||
}
|
||||
27
assignment4/Question5/Start.java
Normal file
27
assignment4/Question5/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
64
assignment4/Question5/StartGrading.java
Normal file
64
assignment4/Question5/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
58
assignment4/Question6/Base.java
Normal file
58
assignment4/Question6/Base.java
Normal 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);
|
||||
}
|
||||
}
|
||||
113
assignment4/Question6/Course.java
Normal file
113
assignment4/Question6/Course.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
13
assignment4/Question6/Learnable.java
Normal file
13
assignment4/Question6/Learnable.java
Normal 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();
|
||||
}
|
||||
56
assignment4/Question6/MajorElective.java
Normal file
56
assignment4/Question6/MajorElective.java
Normal 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);
|
||||
}
|
||||
}
|
||||
57
assignment4/Question6/MajorRequired.java
Normal file
57
assignment4/Question6/MajorRequired.java
Normal 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);
|
||||
}
|
||||
}
|
||||
51
assignment4/Question6/ManyCourses.java
Normal file
51
assignment4/Question6/ManyCourses.java
Normal 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();
|
||||
}
|
||||
}
|
||||
28
assignment4/Question6/Start.java
Normal file
28
assignment4/Question6/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
76
assignment4/Question6/StartGrading.java
Normal file
76
assignment4/Question6/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
15
assignment5/Question1/BadSpeedSetting.java
Normal file
15
assignment5/Question1/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question1/ExceedSpeedLimit.java
Normal file
15
assignment5/Question1/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question1/Movable.java
Normal file
9
assignment5/Question1/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question1/NotEnoughSpeed.java
Normal file
15
assignment5/Question1/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
5
assignment5/Question1/Start.java
Normal file
5
assignment5/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
}
|
||||
}
|
||||
10
assignment5/Question1/StartGrading.java
Normal file
10
assignment5/Question1/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question1/Vehicle.java
Normal file
180
assignment5/Question1/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question2/BadSpeedSetting.java
Normal file
15
assignment5/Question2/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question2/Car.java
Normal file
130
assignment5/Question2/Car.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-24
|
||||
* Description: Car class
|
||||
*/
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
/**
|
||||
* Car can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param speedLimit The speed limit of the car.
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed
|
||||
* is greater than the speed limit.
|
||||
*/
|
||||
public Car(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Default speed limit is 120.
|
||||
*
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than
|
||||
* the speed limit 120.
|
||||
*/
|
||||
public Car(int speed) throws BadSpeedSetting {
|
||||
this(120, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testCar() {
|
||||
try {
|
||||
Car c1;
|
||||
try {
|
||||
c1 = new Car(-1, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, 121);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
c1 = new Car(120, 100);
|
||||
System.out.println(c1.getSpeed() == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 110);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 120);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c1.accelerate(-20) == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 80);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 60);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 40);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 20);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 0);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
|
||||
Car c2 = new Car(100);
|
||||
System.out.println(c2.getSpeed() == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 110);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 120);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c2.accelerate(-20) == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 80);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 60);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 40);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 20);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 0);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error uncatched exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question2/ExceedSpeedLimit.java
Normal file
15
assignment5/Question2/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question2/Movable.java
Normal file
9
assignment5/Question2/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question2/NotEnoughSpeed.java
Normal file
15
assignment5/Question2/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
6
assignment5/Question2/Start.java
Normal file
6
assignment5/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
}
|
||||
}
|
||||
58
assignment5/Question2/StartGrading.java
Normal file
58
assignment5/Question2/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question2/Vehicle.java
Normal file
180
assignment5/Question2/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question3/Aircraft.java
Normal file
127
assignment5/Question3/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/BadAltitudeSetting.java
Normal file
15
assignment5/Question3/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/BadSpeedSetting.java
Normal file
15
assignment5/Question3/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question3/Car.java
Normal file
130
assignment5/Question3/Car.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-24
|
||||
* Description: Car class
|
||||
*/
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
/**
|
||||
* Car can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param speedLimit The speed limit of the car.
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed
|
||||
* is greater than the speed limit.
|
||||
*/
|
||||
public Car(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Default speed limit is 120.
|
||||
*
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than
|
||||
* the speed limit 120.
|
||||
*/
|
||||
public Car(int speed) throws BadSpeedSetting {
|
||||
this(120, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testCar() {
|
||||
try {
|
||||
Car c1;
|
||||
try {
|
||||
c1 = new Car(-1, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, 121);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
c1 = new Car(120, 100);
|
||||
System.out.println(c1.getSpeed() == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 110);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 120);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c1.accelerate(-20) == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 80);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 60);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 40);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 20);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 0);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
|
||||
Car c2 = new Car(100);
|
||||
System.out.println(c2.getSpeed() == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 110);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 120);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c2.accelerate(-20) == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 80);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 60);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 40);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 20);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 0);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error uncatched exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question3/ExceedSpeedLimit.java
Normal file
15
assignment5/Question3/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question3/Movable.java
Normal file
9
assignment5/Question3/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question3/NotEnoughSpeed.java
Normal file
15
assignment5/Question3/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
7
assignment5/Question3/Start.java
Normal file
7
assignment5/Question3/Start.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
}
|
||||
}
|
||||
125
assignment5/Question3/StartGrading.java
Normal file
125
assignment5/Question3/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
180
assignment5/Question3/Vehicle.java
Normal file
180
assignment5/Question3/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question4/Aircraft.java
Normal file
127
assignment5/Question4/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/BadAltitudeSetting.java
Normal file
15
assignment5/Question4/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/BadSpeedSetting.java
Normal file
15
assignment5/Question4/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question4/Car.java
Normal file
130
assignment5/Question4/Car.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-24
|
||||
* Description: Car class
|
||||
*/
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
/**
|
||||
* Car can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param speedLimit The speed limit of the car.
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed
|
||||
* is greater than the speed limit.
|
||||
*/
|
||||
public Car(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Default speed limit is 120.
|
||||
*
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than
|
||||
* the speed limit 120.
|
||||
*/
|
||||
public Car(int speed) throws BadSpeedSetting {
|
||||
this(120, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testCar() {
|
||||
try {
|
||||
Car c1;
|
||||
try {
|
||||
c1 = new Car(-1, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, 121);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
c1 = new Car(120, 100);
|
||||
System.out.println(c1.getSpeed() == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 110);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 120);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c1.accelerate(-20) == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 80);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 60);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 40);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 20);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 0);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
|
||||
Car c2 = new Car(100);
|
||||
System.out.println(c2.getSpeed() == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 110);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 120);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c2.accelerate(-20) == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 80);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 60);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 40);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 20);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 0);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error uncatched exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/ExceedSpeedLimit.java
Normal file
15
assignment5/Question4/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question4/Movable.java
Normal file
9
assignment5/Question4/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question4/NotEnoughSpeed.java
Normal file
15
assignment5/Question4/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
8
assignment5/Question4/Start.java
Normal file
8
assignment5/Question4/Start.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
Train.testTrain();
|
||||
}
|
||||
}
|
||||
164
assignment5/Question4/StartGrading.java
Normal file
164
assignment5/Question4/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
71
assignment5/Question4/Train.java
Normal file
71
assignment5/Question4/Train.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question4/TrainSpeedChange.java
Normal file
15
assignment5/Question4/TrainSpeedChange.java
Normal 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);
|
||||
}
|
||||
}
|
||||
180
assignment5/Question4/Vehicle.java
Normal file
180
assignment5/Question4/Vehicle.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
127
assignment5/Question5/Aircraft.java
Normal file
127
assignment5/Question5/Aircraft.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/BadAltitudeSetting.java
Normal file
15
assignment5/Question5/BadAltitudeSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/BadSpeedSetting.java
Normal file
15
assignment5/Question5/BadSpeedSetting.java
Normal 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);
|
||||
}
|
||||
}
|
||||
130
assignment5/Question5/Car.java
Normal file
130
assignment5/Question5/Car.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-24
|
||||
* Description: Car class
|
||||
*/
|
||||
|
||||
public class Car extends Vehicle {
|
||||
|
||||
/**
|
||||
* Car can not fly.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean canFly() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param speedLimit The speed limit of the car.
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speedLimit or speed is negative. Or if the speed
|
||||
* is greater than the speed limit.
|
||||
*/
|
||||
public Car(int speedLimit, int speed) throws BadSpeedSetting {
|
||||
super(speedLimit, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Default speed limit is 120.
|
||||
*
|
||||
* @param speed the speed of the car.
|
||||
* @thorws BadSpeedSetting if speed is negative. Or if the speed is greater than
|
||||
* the speed limit 120.
|
||||
*/
|
||||
public Car(int speed) throws BadSpeedSetting {
|
||||
this(120, speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testCar() {
|
||||
try {
|
||||
Car c1;
|
||||
try {
|
||||
c1 = new Car(-1, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, -1);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
||||
}
|
||||
try {
|
||||
c1 = new Car(120, 121);
|
||||
} catch (BadSpeedSetting e) {
|
||||
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
|
||||
}
|
||||
c1 = new Car(120, 100);
|
||||
System.out.println(c1.getSpeed() == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 110);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(10) == 120);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c1.accelerate(-20) == 100);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 80);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 60);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 40);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 20);
|
||||
System.out.println(c1.canFly() == false);
|
||||
System.out.println(c1.accelerate(-20) == 0);
|
||||
System.out.println(c1.canFly() == false);
|
||||
try {
|
||||
c1.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
|
||||
Car c2 = new Car(100);
|
||||
System.out.println(c2.getSpeed() == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 110);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(10) == 120);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(10);
|
||||
} catch (ExceedSpeedLimit e) {
|
||||
System.out.println(e.getMessage().equals(
|
||||
"Current speed is 120. Accelerate 10 will exceed the speed limit!"));
|
||||
}
|
||||
System.out.println(c2.accelerate(-20) == 100);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 80);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 60);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 40);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 20);
|
||||
System.out.println(c2.canFly() == false);
|
||||
System.out.println(c2.accelerate(-20) == 0);
|
||||
System.out.println(c2.canFly() == false);
|
||||
try {
|
||||
c2.accelerate(-20);
|
||||
} catch (NotEnoughSpeed e) {
|
||||
System.out.println(e.getMessage()
|
||||
.equals("Current speed is 0, not enough speed to decelerate!"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error uncatched exception: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/ExceedSpeedLimit.java
Normal file
15
assignment5/Question5/ExceedSpeedLimit.java
Normal 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);
|
||||
}
|
||||
}
|
||||
58
assignment5/Question5/ManyVehicles.java
Normal file
58
assignment5/Question5/ManyVehicles.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
9
assignment5/Question5/Movable.java
Normal file
9
assignment5/Question5/Movable.java
Normal 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;
|
||||
}
|
||||
15
assignment5/Question5/NotEnoughSpeed.java
Normal file
15
assignment5/Question5/NotEnoughSpeed.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
assignment5/Question5/Start.java
Normal file
9
assignment5/Question5/Start.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Vehicle.testVehicle();
|
||||
Car.testCar();
|
||||
Aircraft.testAircraft();
|
||||
Train.testTrain();
|
||||
ManyVehicles.testManyVehicles();
|
||||
}
|
||||
}
|
||||
190
assignment5/Question5/StartGrading.java
Normal file
190
assignment5/Question5/StartGrading.java
Normal 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();
|
||||
}
|
||||
}
|
||||
71
assignment5/Question5/Train.java
Normal file
71
assignment5/Question5/Train.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
assignment5/Question5/TrainSpeedChange.java
Normal file
15
assignment5/Question5/TrainSpeedChange.java
Normal 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);
|
||||
}
|
||||
}
|
||||
180
assignment5/Question5/Vehicle.java
Normal file
180
assignment5/Question5/Vehicle.java
Normal 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
Reference in New Issue
Block a user