a3 question2

This commit is contained in:
2022-04-11 14:06:40 +08:00
parent 8fe0ec8e12
commit f35a39f138
8 changed files with 293 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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