In: Computer Science
Build a class called “Animal” which has three attributes: “ArrayList<String> diet”, “int numOfLegs”, and “Boolean carnivore”. Build a constructor with three parameters to set the three attributes. In the constructor, if the number of legs given in the parameter is below 0 set the number of legs equal to 0. Add seven methods:
Now that we have a base class, we’re going to make a subclass of our “Animal” class. Make a class called “Cat” that is a subclass of “Animal”. In “Cat” add a new attribute “String color”. Build a constructor with two parameters, an arrayList<String> diet and String color, in the new constructor call the superClass’s constructor and assume the number of legs of all cats is 4 and every cat is a carnivore. Add a method “String getColor()” that returns the cat’s color. Replace (override) the original “makeSound()” method with a new “makeSound()” function that returns the string “Meow”.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Animal.java
import java.util.ArrayList;
public class Animal {
//Declaring instance variables
private ArrayList<String> diet;
private int numOfLegs;
private boolean carnivore;
/**
* @param diet
* @param numOfLegs
* @param carnivore
*/
public Animal(ArrayList<String> diet, int
numOfLegs, boolean carnivore) {
this.diet = diet;
if (numOfLegs < 0)
this.numOfLegs =
0;
else
this.numOfLegs =
numOfLegs;
this.carnivore = carnivore;
}
//This method will add item to the Animal Diet
ArrayList
public void addToDiet(String item) {
diet.add(item);
}
void addToDiet(ArrayList<String> items)
{
for (int i = 0; i <
items.size(); i++) {
diet.add(items.get(i));
}
}
void setDiet(ArrayList<String> newDiet)
{
this.diet = newDiet;
}
public ArrayList<String> getDiet() {
return diet;
}
public boolean isCarnivore() {
if (carnivore)
return
true;
else
return
false;
}
public int getNumOfLegs() {
return numOfLegs;
}
public String makeSound() {
return "BOw Bow";
}
}
=====================================
// Cat.java
import java.util.ArrayList;
public class Cat extends Animal {
private String color;
/**
* @param diet
* @param numOfLegs
* @param carnivore
* @param color
*/
public Cat(ArrayList<String> diet, String color)
{
super(diet, 4, true);
this.color = color;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color
* the color to set
*/
public void setColor(String color) {
this.color = color;
}
@Override
public String makeSound() {
return "Meow";
}
}
=================================
// Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args)
{
ArrayList<String> diet = new
ArrayList<String>();
diet.add("Chicken");
diet.add("Meat");
//Creating an
instance of Animal class
Cat a = new
Cat(diet,"White");
//Adding item to the animal
diet
a.addToDiet("Biscuits");
System.out.println("Animal Diet :");
ArrayList<String> arl =
a.getDiet();
for (int i = 0; i < arl.size();
i++) {
System.out.println(arl.get(i));
}
System.out.println("No of Legs
:"+a.getNumOfLegs());
System.out.println("Cat color
:"+a.getColor());
System.out.println("Cat makes
\""+a.makeSound()+"\" sound");
boolean b=a.isCarnivore();
if(b)
{
System.out.println("Cat is carnivore");
}
else
{
System.out.println("Cat is not carnivore");
}
}
}
========================
Output:
Animal Diet :
Chicken
Meat
Biscuits
No of Legs :4
Cat color :White
Cat makes "Meow" sound
Cat is carnivore
=====================Could you plz rate me
well.Thank You