In: Computer Science
Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).
The Animal class has the following protected instance variables:
boolean vegetarian, String eatings, int numOfLegs and the following public instance methods:
constructor without parameters: initialize all of the instance variables to some default values
constructor with parameters: initialize all of the instance variables to the arguments
SetAnimal: assign arguments to all of the instance variables
Three “Get” methods which retrieve the respective values of the instance variables
toString: Returns the animal’s vegetarian, eatings and numOfLegs information as a string
The Cat class has the following private instance variable:
String color and the following public instance methods:
constructor without parameters: initialize all of the instance variables to some default values, including its super class - Animal’s instance variables
constructor with parameters: initialize all of the instance variables to the arguments, including its super class Animal’s instance variables
SetColor: assign its instance variable to the argument
GetColor: retrieve the color value
overrided toString: Returns the cat’s vegetarian, eatings, numOfLegs and color information as a string
Please write your complete Animal class, Cat class and a driver class as required below
a (35 pts) Write your complete Java code for the Animal class in Animal.java file
b (30 pts) Write your complete Java code for the Cat class in Cat.java file
b (35 pts) Write your test Java class and its main method which will create two Cats instances: e1 and e2, e1 is created with the default constructor, e2 is created with the explicit value constructor. Then update e1 to reset its vegetarian, eatings, numOfLegs and color. Output both cats’ detailed information. The above test Java class should be written in a Java file named testAnimal.java.
class Animal
{
protected boolean vegetarian;
protected String eatings;
protected int numOfLegs;
public Animal()
{
vegetarian = false;
eatings = "meat";
numOfLegs = 4;
}
public Animal(boolean vegetarian,String eatings, int
numOfLegs)
{
this.vegetarian = vegetarian;
this.eatings = eatings;
this.numOfLegs = numOfLegs;
}
public void SetAnimal(boolean vegetarian,String eatings, int
numOfLegs)
{
this.vegetarian = vegetarian;
this.eatings = eatings;
this.numOfLegs = numOfLegs;
}
public boolean getVegetarian()
{
return vegetarian;
}
public String getEatings()
{
return eatings;
}
public int getNumOfLegs()
{
return numOfLegs;
}
public String toString()
{
return "Animal’s vegetarian : "+ vegetarian +", eatings : "+
eatings+" number of legs : "+numOfLegs;
}
}
class Cat extends Animal
{
private String color;
public Cat()
{
super();
}
public Cat(boolean vegetarian,String eatings, int numOfLegs,
String color)
{
super(vegetarian,eatings,numOfLegs);
this.color = color;
}
public void SetColor(String color)
{
this.color = color;
}
public String GetColor()
{
return color;
}
public String toString()
{
return super.toString() +" color : "+color;
}
}
class testAnimal
{
public static void main (String[] args)
{
Cat e1 = new Cat();
Cat e2 = new
Cat(false,"meat,milk",4, "white");
e1.SetAnimal(true,"grass",4);
e1.SetColor("black");
System.out.println(e1);
System.out.println(e2);
}
}
Output:
Animal’s vegetarian : true, eatings : grass number of legs : 4 color : black Animal’s vegetarian : false, eatings : meat,milk number of legs : 4 color : white
Do ask if any doubt. Please up-vote.