Merge branch 'a3' close #3 Assignment 3
This commit is contained in:
24
assignment2/Question1/HalfMarathon.java
Normal file
24
assignment2/Question1/HalfMarathon.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class HalfMarathon extends Marathon {
|
||||
/**
|
||||
* Constructor. Half marathon always has player number 99 and length of 21.0975
|
||||
*/
|
||||
public HalfMarathon() {
|
||||
super(99, 21.0975);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testHalfMarathon() {
|
||||
HalfMarathon hm = new HalfMarathon();
|
||||
System.out.println(hm.getLength() == 21.0975);
|
||||
System.out.println(hm.getPlayerNumber() == 99);
|
||||
System.out.println(hm.isFun() == false);
|
||||
}
|
||||
}
|
||||
43
assignment2/Question1/Marathon.java
Normal file
43
assignment2/Question1/Marathon.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class Marathon extends Running {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber
|
||||
* the player number
|
||||
* @param length
|
||||
* the length of the marathon
|
||||
*/
|
||||
public Marathon(int playerNumber, double length) {
|
||||
super(playerNumber, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Always set the length to 42.195
|
||||
*
|
||||
* @param playerNumber
|
||||
*/
|
||||
public Marathon(int playerNumber) {
|
||||
this(playerNumber, 42.195);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMarathon() {
|
||||
Marathon m1 = new Marathon(1, 39.39);
|
||||
System.out.println(m1.getLength() == 39.39);
|
||||
System.out.println(m1.getPlayerNumber() == 1);
|
||||
System.out.println(m1.isFun() == false);
|
||||
|
||||
Marathon m2 = new Marathon(2);
|
||||
System.out.println(m2.getLength() == 42.195);
|
||||
System.out.println(m2.getPlayerNumber() == 2);
|
||||
System.out.println(m2.isFun() == false);
|
||||
}
|
||||
}
|
||||
50
assignment2/Question1/Running.java
Normal file
50
assignment2/Question1/Running.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class Running extends Sport {
|
||||
/**
|
||||
* The length of the running.
|
||||
*/
|
||||
private double length;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber the player number of the running.
|
||||
* @param length the length of the running
|
||||
*/
|
||||
public Running(int playerNumber, double length) {
|
||||
super(playerNumber);
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the length of the running.
|
||||
*/
|
||||
public double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Running is not fun.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean isFun() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testRunning() {
|
||||
Running r1 = new Running(1, 1.5);
|
||||
System.out.println(r1.getPlayerNumber() == 1);
|
||||
System.out.println(r1.getLength() == 1.5);
|
||||
System.out.println(r1.isFun() == false);
|
||||
}
|
||||
}
|
||||
46
assignment2/Question1/Sport.java
Normal file
46
assignment2/Question1/Sport.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Sport class.
|
||||
*/
|
||||
|
||||
public class Sport {
|
||||
private int playerNumber;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber
|
||||
*/
|
||||
public Sport(int playerNumber) {
|
||||
this.playerNumber = playerNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for playerNumber.
|
||||
*
|
||||
* @return playerNumber
|
||||
*/
|
||||
public int getPlayerNumber() {
|
||||
return playerNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the sport is fun or not. Some sport are fun and some sports are not
|
||||
* fun. By default the sport is not fun.
|
||||
*
|
||||
* @return true if the sport is fun, false if the sport is not fun.
|
||||
*/
|
||||
public boolean isFun() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testSport() {
|
||||
Sport sport = new Sport(10);
|
||||
System.out.println(sport.getPlayerNumber() == 10);
|
||||
System.out.println(sport.isFun() == false);
|
||||
}
|
||||
}
|
||||
9
assignment2/Question1/Start.java
Normal file
9
assignment2/Question1/Start.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Sport.testSport();
|
||||
Tennis.testTennis();
|
||||
Running.testRunning();
|
||||
Marathon.testMarathon();
|
||||
HalfMarathon.testHalfMarathon();
|
||||
}
|
||||
}
|
||||
33
assignment2/Question1/Tennis.java
Normal file
33
assignment2/Question1/Tennis.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Tennis class which is the subclass of the Sport class.
|
||||
*/
|
||||
|
||||
public class Tennis extends Sport {
|
||||
/**
|
||||
* Constructor. Tennis has 2 player.
|
||||
*/
|
||||
public Tennis() {
|
||||
super(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tennis is fun.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean isFun() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testTennis() {
|
||||
Tennis t = new Tennis();
|
||||
System.out.println(t.getPlayerNumber() == 2);
|
||||
System.out.println(t.isFun() == true);
|
||||
}
|
||||
}
|
||||
24
assignment2/Question2/HalfMarathon.java
Normal file
24
assignment2/Question2/HalfMarathon.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class HalfMarathon extends Marathon {
|
||||
/**
|
||||
* Constructor. Half marathon always has player number 99 and length of 21.0975
|
||||
*/
|
||||
public HalfMarathon() {
|
||||
super(99, 21.0975);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testHalfMarathon() {
|
||||
HalfMarathon hm = new HalfMarathon();
|
||||
System.out.println(hm.getLength() == 21.0975);
|
||||
System.out.println(hm.getPlayerNumber() == 99);
|
||||
System.out.println(hm.isFun() == false);
|
||||
}
|
||||
}
|
||||
43
assignment2/Question2/Marathon.java
Normal file
43
assignment2/Question2/Marathon.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class Marathon extends Running {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber
|
||||
* the player number
|
||||
* @param length
|
||||
* the length of the marathon
|
||||
*/
|
||||
public Marathon(int playerNumber, double length) {
|
||||
super(playerNumber, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Always set the length to 42.195
|
||||
*
|
||||
* @param playerNumber
|
||||
*/
|
||||
public Marathon(int playerNumber) {
|
||||
this(playerNumber, 42.195);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMarathon() {
|
||||
Marathon m1 = new Marathon(1, 39.39);
|
||||
System.out.println(m1.getLength() == 39.39);
|
||||
System.out.println(m1.getPlayerNumber() == 1);
|
||||
System.out.println(m1.isFun() == false);
|
||||
|
||||
Marathon m2 = new Marathon(2);
|
||||
System.out.println(m2.getLength() == 42.195);
|
||||
System.out.println(m2.getPlayerNumber() == 2);
|
||||
System.out.println(m2.isFun() == false);
|
||||
}
|
||||
}
|
||||
50
assignment2/Question2/Running.java
Normal file
50
assignment2/Question2/Running.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Running class which is the subclass of the Sport class
|
||||
*/
|
||||
|
||||
public class Running extends Sport {
|
||||
/**
|
||||
* The length of the running.
|
||||
*/
|
||||
private double length;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber the player number of the running.
|
||||
* @param length the length of the running
|
||||
*/
|
||||
public Running(int playerNumber, double length) {
|
||||
super(playerNumber);
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the length of the running.
|
||||
*/
|
||||
public double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Running is not fun.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public boolean isFun() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testRunning() {
|
||||
Running r1 = new Running(1, 1.5);
|
||||
System.out.println(r1.getPlayerNumber() == 1);
|
||||
System.out.println(r1.getLength() == 1.5);
|
||||
System.out.println(r1.isFun() == false);
|
||||
}
|
||||
}
|
||||
46
assignment2/Question2/Sport.java
Normal file
46
assignment2/Question2/Sport.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Sport class.
|
||||
*/
|
||||
|
||||
public class Sport {
|
||||
private int playerNumber;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param playerNumber
|
||||
*/
|
||||
public Sport(int playerNumber) {
|
||||
this.playerNumber = playerNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for playerNumber.
|
||||
*
|
||||
* @return playerNumber
|
||||
*/
|
||||
public int getPlayerNumber() {
|
||||
return playerNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the sport is fun or not. Some sport are fun and some sports are not
|
||||
* fun. By default the sport is not fun.
|
||||
*
|
||||
* @return true if the sport is fun, false if the sport is not fun.
|
||||
*/
|
||||
public boolean isFun() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testSport() {
|
||||
Sport sport = new Sport(10);
|
||||
System.out.println(sport.getPlayerNumber() == 10);
|
||||
System.out.println(sport.isFun() == false);
|
||||
}
|
||||
}
|
||||
37
assignment2/Question2/SportsCourt.java
Normal file
37
assignment2/Question2/SportsCourt.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the class of SportsCourt.
|
||||
*/
|
||||
|
||||
public class SportsCourt {
|
||||
private Sport sport;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param sport
|
||||
*/
|
||||
public SportsCourt(Sport sport) {
|
||||
this.sport = sport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of sport.
|
||||
*
|
||||
* @return sport
|
||||
*/
|
||||
public Sport playSport() {
|
||||
return sport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testSportsCourt() {
|
||||
HalfMarathon hm1 = new HalfMarathon();
|
||||
SportsCourt sc1 = new SportsCourt(hm1);
|
||||
HalfMarathon hm2 = (HalfMarathon) sc1.playSport();
|
||||
System.out.println(sc1.playSport() == hm2);
|
||||
}
|
||||
}
|
||||
10
assignment2/Question2/Start.java
Normal file
10
assignment2/Question2/Start.java
Normal file
@@ -0,0 +1,10 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Sport.testSport();
|
||||
Tennis.testTennis();
|
||||
Running.testRunning();
|
||||
Marathon.testMarathon();
|
||||
HalfMarathon.testHalfMarathon();
|
||||
SportsCourt.testSportsCourt();
|
||||
}
|
||||
}
|
||||
33
assignment2/Question2/Tennis.java
Normal file
33
assignment2/Question2/Tennis.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022/03/04
|
||||
* Description: This is the Tennis class which is the subclass of the Sport class.
|
||||
*/
|
||||
|
||||
public class Tennis extends Sport {
|
||||
/**
|
||||
* Constructor. Tennis has 2 player.
|
||||
*/
|
||||
public Tennis() {
|
||||
super(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tennis is fun.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean isFun() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testTennis() {
|
||||
Tennis t = new Tennis();
|
||||
System.out.println(t.getPlayerNumber() == 2);
|
||||
System.out.println(t.isFun() == true);
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
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() {
|
||||
Rabbit lsc1 = new Rabbit("LSC1", 3.9);
|
||||
CastIronPot cip = new CastIronPot(lsc1);
|
||||
Rabbit lsc2 = 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user