Please show fully functioning java code and outputs.
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
Animal.java
import java.util.*;
class Animal{
boolean vegetarian;
String eatings;
int numOfLegs;
//constructor without parameters
Animal(){
//inititlizing to default values
vegetarian=true;
eatings="";
numOfLegs=4;
}
//constructor with parameters
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;
}
//get methods to return instance variables
public boolean getVegetarian(){
return vegetarian;
}
public String getEatings(){
return eatings;
}
public int getNumOfLegs(){
return numOfLegs;
}
public String toString(){
return "Vegetarian: "+vegetarian+"\nEatings: "+eatings+"\nNumber of legs: "+numOfLegs;
}
}
Cat.java
import java.util.*;
class Cat extends Animal{
String color;
//constructor without parameters
Cat(){
//this initializes super class with default values
super(false, "milk", 4);
//inititlizing to default values
color="black";
}
//constructor with parameters
Cat(String color, boolean vegetarian, String eatings, int numOfLegs){
super(vegetarian, eatings, numOfLegs);
this.color=color;
}
public void setColor(String color){
this.color=color;
}
//get methods to return instance variables
public String getColor(){
return color;
}
public String toString(){
return "Vegetarian: "+vegetarian+"\nEatings: "+eatings+"\nNumber of legs: "+numOfLegs + "\nColors: "+color;
}
}
Tester.java
import java.util.*;
class Tester{
public static void main(String[] args){
Animal x = new Animal();
System.out.println("Animal with Default constructor:\n" + x);
x = new Animal(false, "grass", 4);
System.out.println("Animal with Parameterized constructor:\n" + x);
Cat c = new Cat();
System.out.println("Cat with Default constructor:\n" + c);
c = new Cat("white", false, "meat", 4);
System.out.println("Cat with Parameterized constructor:\n" + c);
}
}
Sample Console Output
Animal with Default constructor:
Vegetarian: true
Eatings:
Number of legs: 4
Animal with Parameterized constructor:
Vegetarian: false
Eatings: grass
Number of legs: 4
Cat with Default constructor:
Vegetarian: false
Eatings: milk
Number of legs: 4
Colors: black
Cat with Parameterized constructor:
Vegetarian: false
Eatings: meat
Number of legs: 4
Colors: white
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.
Get Answers For Free
Most questions answered within 1 hours.