In: Computer Science
JAVA programming
A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose.
Part A: Think through common characteristics of animals, and write a class ZooAnimal.
☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for.
☑ It should also have at least two methods for behaviors that all animals do; these can simply print out a String stating what the animal is doing. (accessors and mutators are not behaviors). Include at least one parameterized constructor.
Part B ☑ Create classes Elephant, Camel, and Moose, all of which extend ZooAnimal. In each, include a default constructor that calls the parameterized superclass constructor.
☑ To one class (of Elephant, Camel, and Moose), add another instance variable specific to that type of animal. To another, add another behavior method. In a third, override a behavior from ZooAnimal.
Part C ☑ Create a test harness for the animal classes. In a main create at least one Elephant, Camel, and Moose, and show which behavior methods are usable for each.
☑ [EC+10] Create another animal class inheriting from one of your three types (doesn't have to be zoologically accurate, but should make some sense), and either add an instance variable or add or override a method. This class should also be tested in the harness.
Explanation:I have written all the classes ZooAnimal,Moose,Camel,Cow,Elephant and Harness as per the requirements mentioned.I have shown the the output of the program, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.
//ZooAnimal class
public class ZooAnimal {
protected String name;
protected String primaryColor;
public ZooAnimal() {
super();
}
public ZooAnimal(String name, String primaryColor)
{
super();
this.name = name;
this.primaryColor =
primaryColor;
}
public void moving() {
System.out.println(name + " is
moving");
}
public void sleeping() {
System.out.println(name + " is
sleeping");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrimaryColor() {
return primaryColor;
}
public void setPrimaryColor(String primaryColor)
{
this.primaryColor =
primaryColor;
}
@Override
public String toString() {
return "name=" + name + ",
primaryColor=" + primaryColor;
}
}
//Elephant class
public class Elephant extends ZooAnimal {
protected boolean isWild;
public Elephant() {
super();
}
public Elephant(String name, String primaryColor,
boolean isWild) {
super(name, primaryColor);
this.isWild = isWild;
}
public boolean isWild() {
return isWild;
}
public void setWild(boolean isWild) {
this.isWild = isWild;
}
@Override
public String toString() {
return "Elephant [" +
super.toString() + ", isWild=" + isWild + "]";
}
}
//Camel class
public class Camel extends ZooAnimal {
public Camel() {
}
public Camel(String name, String primaryColor)
{
super(name, primaryColor);
}
public void eating() {
System.out.println(name + " is
eating");
}
@Override
public String toString() {
return "Camel [name=" + name + ",
primaryColor=" + primaryColor + "]";
}
}
//Moose class
public class Moose extends ZooAnimal {
public Moose() {
super();
}
public Moose(String name, String primaryColor)
{
super(name, primaryColor);
}
@Override
public void sleeping() {
System.out.println("Moose " + name
+ " is sleeping");
}
@Override
public String toString() {
return "Moose [name=" + name + ",
primaryColor=" + primaryColor + "]";
}
}
//Cow class
public class Cow extends Moose {
protected boolean givesMilk;
public Cow() {
}
public Cow(String name, String primaryColor,
boolean givesMilk) {
super(name, primaryColor);
this.givesMilk = givesMilk;
}
public boolean isGivesMilk() {
return givesMilk;
}
public void setGivesMilk(boolean givesMilk) {
this.givesMilk = givesMilk;
}
@Override
public String toString() {
return "Cow [ name=" + name + ",
primaryColor=" + primaryColor
+ ", givesMilk=" + givesMilk + "]";
}
}
//Harness class
public class Harness {
public static void main(String[] args) {
Elephant elephant = new
Elephant("Tommy", "black", false);
System.out.println(elephant.toString());
Moose moose = new Moose("Ratty",
"gray");
System.out.println(moose.toString());
moose.moving();
Camel camel = new Camel("Tora",
"yellow");
System.out.println(camel.toString());
camel.eating();
Cow cow = new Cow("Teeti", "white",
true);
System.out.println(cow.toString());
}
}
Outputs: