diff --git a/assignment2/Question1/HalfMarathon.java b/assignment2/Question1/HalfMarathon.java new file mode 100644 index 0000000..ca28cbb --- /dev/null +++ b/assignment2/Question1/HalfMarathon.java @@ -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); + } +} diff --git a/assignment2/Question1/Marathon.java b/assignment2/Question1/Marathon.java new file mode 100644 index 0000000..29d3102 --- /dev/null +++ b/assignment2/Question1/Marathon.java @@ -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); + } +} diff --git a/assignment2/Question1/Running.java b/assignment2/Question1/Running.java new file mode 100644 index 0000000..5b1b77e --- /dev/null +++ b/assignment2/Question1/Running.java @@ -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); + } +} diff --git a/assignment2/Question1/Sport.java b/assignment2/Question1/Sport.java new file mode 100644 index 0000000..ac50875 --- /dev/null +++ b/assignment2/Question1/Sport.java @@ -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); + } +} diff --git a/assignment2/Question1/Start.java b/assignment2/Question1/Start.java new file mode 100644 index 0000000..f384976 --- /dev/null +++ b/assignment2/Question1/Start.java @@ -0,0 +1,9 @@ +public class Start { + public static void main(String[] args) { + Sport.testSport(); + Tennis.testTennis(); + Running.testRunning(); + Marathon.testMarathon(); + HalfMarathon.testHalfMarathon(); + } +} diff --git a/assignment2/Question1/Tennis.java b/assignment2/Question1/Tennis.java new file mode 100644 index 0000000..47b5088 --- /dev/null +++ b/assignment2/Question1/Tennis.java @@ -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); + } +} diff --git a/assignment2/Question2/HalfMarathon.java b/assignment2/Question2/HalfMarathon.java new file mode 100644 index 0000000..ca28cbb --- /dev/null +++ b/assignment2/Question2/HalfMarathon.java @@ -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); + } +} diff --git a/assignment2/Question2/Marathon.java b/assignment2/Question2/Marathon.java new file mode 100644 index 0000000..29d3102 --- /dev/null +++ b/assignment2/Question2/Marathon.java @@ -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); + } +} diff --git a/assignment2/Question2/Running.java b/assignment2/Question2/Running.java new file mode 100644 index 0000000..5b1b77e --- /dev/null +++ b/assignment2/Question2/Running.java @@ -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); + } +} diff --git a/assignment2/Question2/Sport.java b/assignment2/Question2/Sport.java new file mode 100644 index 0000000..ac50875 --- /dev/null +++ b/assignment2/Question2/Sport.java @@ -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); + } +} diff --git a/assignment2/Question2/SportsCourt.java b/assignment2/Question2/SportsCourt.java new file mode 100644 index 0000000..03954e5 --- /dev/null +++ b/assignment2/Question2/SportsCourt.java @@ -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); + } +} diff --git a/assignment2/Question2/Start.java b/assignment2/Question2/Start.java new file mode 100644 index 0000000..95c4990 --- /dev/null +++ b/assignment2/Question2/Start.java @@ -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(); + } +} diff --git a/assignment2/Question2/Tennis.java b/assignment2/Question2/Tennis.java new file mode 100644 index 0000000..47b5088 --- /dev/null +++ b/assignment2/Question2/Tennis.java @@ -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); + } +} diff --git a/assignment3/Question1/EuropeanRabbit.java b/assignment3/Question1/EuropeanRabbit.java new file mode 100644 index 0000000..540cb5c --- /dev/null +++ b/assignment3/Question1/EuropeanRabbit.java @@ -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); + } +} diff --git a/assignment3/Question1/FrankTheRabbit.java b/assignment3/Question1/FrankTheRabbit.java new file mode 100644 index 0000000..96c14fa --- /dev/null +++ b/assignment3/Question1/FrankTheRabbit.java @@ -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); + } +} diff --git a/assignment3/Question1/Human.java b/assignment3/Question1/Human.java new file mode 100644 index 0000000..e39286d --- /dev/null +++ b/assignment3/Question1/Human.java @@ -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); + } +} diff --git a/assignment3/Question1/LapinSauteChasseur.java b/assignment3/Question1/LapinSauteChasseur.java new file mode 100644 index 0000000..216f06a --- /dev/null +++ b/assignment3/Question1/LapinSauteChasseur.java @@ -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); + } +} diff --git a/assignment3/Question1/Mammal.java b/assignment3/Question1/Mammal.java new file mode 100644 index 0000000..1f76242 --- /dev/null +++ b/assignment3/Question1/Mammal.java @@ -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); + } +} diff --git a/assignment3/Question1/Rabbit.java b/assignment3/Question1/Rabbit.java new file mode 100644 index 0000000..c1d8063 --- /dev/null +++ b/assignment3/Question1/Rabbit.java @@ -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); + } +} diff --git a/assignment3/Question1/Start.java b/assignment3/Question1/Start.java new file mode 100644 index 0000000..6bf9d3b --- /dev/null +++ b/assignment3/Question1/Start.java @@ -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(); + } +} diff --git a/assignment3/Question2/CastIronPot.java b/assignment3/Question2/CastIronPot.java new file mode 100644 index 0000000..2f189cc --- /dev/null +++ b/assignment3/Question2/CastIronPot.java @@ -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); + } +} diff --git a/assignment3/Question2/EuropeanRabbit.java b/assignment3/Question2/EuropeanRabbit.java new file mode 100644 index 0000000..540cb5c --- /dev/null +++ b/assignment3/Question2/EuropeanRabbit.java @@ -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); + } +} diff --git a/assignment3/Question2/FrankTheRabbit.java b/assignment3/Question2/FrankTheRabbit.java new file mode 100644 index 0000000..96c14fa --- /dev/null +++ b/assignment3/Question2/FrankTheRabbit.java @@ -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); + } +} diff --git a/assignment3/Question2/Human.java b/assignment3/Question2/Human.java new file mode 100644 index 0000000..e39286d --- /dev/null +++ b/assignment3/Question2/Human.java @@ -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); + } +} diff --git a/assignment3/Question2/LapinSauteChasseur.java b/assignment3/Question2/LapinSauteChasseur.java new file mode 100644 index 0000000..216f06a --- /dev/null +++ b/assignment3/Question2/LapinSauteChasseur.java @@ -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); + } +} diff --git a/assignment3/Question2/Mammal.java b/assignment3/Question2/Mammal.java new file mode 100644 index 0000000..1f76242 --- /dev/null +++ b/assignment3/Question2/Mammal.java @@ -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); + } +} diff --git a/assignment3/Question2/Rabbit.java b/assignment3/Question2/Rabbit.java new file mode 100644 index 0000000..c1d8063 --- /dev/null +++ b/assignment3/Question2/Rabbit.java @@ -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); + } +} diff --git a/assignment3/Question2/Start.java b/assignment3/Question2/Start.java new file mode 100644 index 0000000..3094561 --- /dev/null +++ b/assignment3/Question2/Start.java @@ -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(); + } +}