In: Computer Science
Classwork
Java Program
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.
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.
Here is the code for above problem statement along with outout screenshot. Please comment if need any further clarifications. Also do upvote if i answered your question.
-----------------------------------ZooAnimal----------------------------------
/**
* The Class ZooAnimal.
*/
public class ZooAnimal {
/** The weight. */
protected double weight;
/** The category. */
protected String category;
/**
* Instantiates a new zoo animal.
*
* @param weight the weight
* @param category the category
*/
public ZooAnimal(double weight, String category)
{
this.weight = weight;
this.category = category;
}
/**
* Eat.
*/
public void eat() {
System.out.println(this.category +
" is eating");
}
/**
* Walk.
*/
public void walk() {
System.out.println(this.category +
" is walking");
}
/**
* Gets the weight.
*
* @return the weight
*/
public double getWeight() {
return weight;
}
/**
* Sets the weight.
*
* @param weight the new weight
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* Gets the category.
*
* @return the category
*/
public String getCategory() {
return category;
}
/**
* Sets the category.
*
* @param category the new category
*/
public void setCategory(String category) {
this.category = category;
}
}
-----------------------------------Elephant---------------------------------------
/**
* The Class Elephant.
*/
public class Elephant extends ZooAnimal {
/**
* Instantiates a new elephant.
*/
public Elephant() {
super(12, "Elephant");
}
public Elephant(String elephantType) {
super(12, elephantType);
}
/**
* Drink.
*/
public void drink() {
System.out.println("Elephant is
drinking");
}
}
------------------------------------Camel----------------------------------------
/**
* The Class Camel.
*/
public class Camel extends ZooAnimal {
/** The camel type. */
private String camelType;
/**
* Instantiates a new camel.
*/
public Camel() {
super(10, "Camel");
}
/**
* Gets the camel type.
*
* @return the camel type
*/
public String getCamelType() {
return camelType;
}
/**
* Sets the camel type.
*
* @param camelType the new camel type
*/
public void setCamelType(String camelType) {
this.camelType = camelType;
}
}
-------------------------------------Moose--------------------------------------
/**
* The Class Moose.
*/
public class Moose extends ZooAnimal {
/**
* Instantiates a new moose.
*/
public Moose() {
super(15, "Moose");
}
/**
* Walk.
*/
public void walk() {
System.out.println("Overridden :
Moose is walking");
}
}
----------------------------------AfricanElephant------------------------------
/**
* The Class AfricanElephant.
*/
public class AfricanElephant extends Elephant {
public AfricanElephant() {
super("African Elephant");
}
/**
* Drink.
*/
public void drink() {
System.out.println("African
Elephant is drinking");
}
}
----------------------------------TestClassZoo------------------------------
/**
* The Class TestClassZoo.
*/
public class TestClassZoo {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Elephant elephant = new
Elephant();
elephant.eat();
elephant.walk();
elephant.drink();
AfricanElephant africanElephant =
new AfricanElephant();
africanElephant.eat();
africanElephant.walk();
africanElephant.drink();
Camel camel = new Camel();
camel.eat();
camel.walk();
Moose moose = new Moose();
moose.eat();
moose.walk();
}
}
----------------------------------------------OUTPUT-------------------------------------------------