In: Computer Science
This is the question about the java problem, please give the detail comment and code of each class.
Please write tests for all the code of all the classes
Thank you
Create a class Mammal with the following UML
diagrams:
+---------------------------------+
| Mammal |
+---------------------------------+
| - name: String |
+---------------------------------+
| + Mammal(String name) |
| + getName(): String |
| + isCookable(): boolean |
| + testMammal(): void | (this method is static
)
+---------------------------------+
The isCookable method of the
Mammal class returns a boolean indicating whether
the mammal can be cooked or not: some mammals can be cooked and
some mammals cannot be cooked so for safety reasons the
isCookable method just prints a message
"Do not cook this!" and returns false (because the
isCookablemethod must return a boolean).
Add a class Human to your program. A human is a
mammal. The constructor for the Human class takes
no argument. All humans are named "Alice" and
humans cannot be cooked (the isCookable method
must not print any message though, it simply returns
false).
Add a class Rabbit to your program. A rabbit is a
mammal. The Rabbit class has a private instance
variable weight of type double
that describes the weight of the rabbit, and a
getWeight method. The constructor for the
Rabbit class takes the rabbit’s name and the
rabbit’s weight as arguments. A rabbit can be cooked.
Add a class EuropeanRabbit to your program.
European rabbit is a species of rabbit. The
EuropeanRabbitclass has two constructors: the
first constructor takes the European rabbit’s name and the European
rabbit’s weight as arguments; the second constructor only takes the
European rabbit’s name as argument and always uses
2.0 as the European rabbit’s weight. The second
constructor must use the first constructor.
Add a class LapinSautéChasseur to your program.
Lapin sauté chasseur is a kind of European rabbit. The constructor
for the LapinSautéChasseur class takes no
argument. Lapin sauté chasseur is always named
"Delicious" and has a weight of 0.5.
Add a class FrankTheRabbit to your program. Frank
The Rabbit is a kind of rabbit. The constructor for
theFrankTheRabbit class takes no argument. Frank
The Rabbit is always named "Frank", has a weight
of 100.0, and cannot be cooked.
Add a class Start to your program to test all your
classes.
Question 2
Add a class CastIronPot to your program with the
following UML diagram:
+---------------------------------+
| CastIronPot |
+---------------------------------+
| - rabbit: Rabbit |
+---------------------------------+
| + CastIronPot(Rabbit rabbit) |
| + getRabbit(): Rabbit |
| + testCastIronPot(): void |(this method is
static)
+---------------------------------+
In the testCastIronPot method, create a lapin
sauté chasseur called lsc1, then create a cast
iron pot with this lapin sauté chasseur in it. Then get the lapin
sauté chasseur from the cast iron pot and store it into a local
variable called lsc2 of type
LapinSautéChasseur. Use the == operator to check
that lsc1 and lsc2 are the same
lapin sauté chasseur.
/**********************Mammal.java******************/
public class Mammal {
private String name;
public Mammal(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public boolean isCookable() {
System.out.println("Do not cook
this!");
return false;
}
public static void testMammal() {
}
}
/*****************************Human.java****************/
public class Human extends Mammal {
public Human() {
super("Alice");
}
@Override
public boolean isCookable() {
return false;
}
}
/*******************************Rabbit.java***************************/
public class Rabbit extends Mammal {
private double weight;
public Rabbit(String name, double weight) {
super(name);
this.weight = weight;
}
public double getWeight() {
return weight;
}
@Override
public boolean isCookable() {
return true;
}
}
/*********************************EuropeanRabbit.java*******************************/
public class EuropeanRabbit extends Rabbit {
public EuropeanRabbit(String name, double weight)
{
super(name, weight);
}
public EuropeanRabbit(String name) {
this(name, 2.0);
}
@Override
public boolean isCookable() {
return true;
}
}
/***********************************LapinSautéChasseur.java************************/
public class LapinSautéChasseur extends EuropeanRabbit {
public LapinSautéChasseur() {
super("Delicious", 0.5);
}
}
/*******************************FrankTheRabbit.java******************************/
public class FrankTheRabbit extends Rabbit {
public FrankTheRabbit() {
super("Frank", 100.0);
}
@Override
public boolean isCookable() {
return false;
}
}
/*************************************CastIronPot.java*************************/
public class CastIronPot {
private Rabbit rabbit;
public CastIronPot(Rabbit rabbit) {
super();
this.rabbit = rabbit;
}
public Rabbit getRabbit() {
return rabbit;
}
public static void testCastIronPot() {
LapinSautéChasseur lsc1 = new
LapinSautéChasseur();
LapinSautéChasseur lsc2 = new
LapinSautéChasseur();
if (lsc1 == lsc2) {
System.out.println("Both are same");
} else {
System.out.println("Both are not same");
}
}
}
/*********************************Start.java*******************/
import java.util.ArrayList;
public class Start {
public static void main(String[] args) {
ArrayList<Mammal> mammals =
new ArrayList<>();
mammals.add(new Human());
mammals.add(new Rabbit("ABC",
3.0));
mammals.add(new
EuropeanRabbit("DFG"));
mammals.add(new
LapinSautéChasseur());
mammals.add(new
FrankTheRabbit());
for (Mammal mammal : mammals)
{
System.out.println(mammal.isCookable());
}
CastIronPot pot = new
CastIronPot(new LapinSautéChasseur());
System.out.println(pot.getRabbit().getName());
CastIronPot.testCastIronPot();
}
}
/************************************output*******************/
false
true
true
true
false
Delicious
Both are not same
Please let me know if you have any doubt or modify the answer, Thanks:)