Question

In: Computer Science

Build a class called “Animal” which has three attributes: “ArrayList<String> diet”, “int numOfLegs”, and “Boolean carnivore”....

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:

  • “void addToDiet(String item)” which will add a single item to the animals diet
  • “void addToDiet(ArrayList<String> items)” which will add all items given to the animals diet
  • “void setDiet(ArrayList<String> newDiet)” which will set the animals diet to be the new diet
  • “ArrayList<String> getDiet()” returns the animals diet
  • “Boolean isCarnivore()” returns if the animal is a carnivore or not
  • “int getNumOfLegs()” returns the animal’s number of legs
  • “String makeSound()” returns the string “Any Sound”

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”.

Solutions

Expert Solution

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


Related Solutions

Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available …………………...
Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available ………………… +constructor, getters, setters, toString() Write a lambda expression showAdoptable using a standard functional interface that will display the dog’s name age and breed if adopted is false. Help please!!
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS FILE...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Account that contains: • Three privateinstance variables: name (String), account (String),...
Write a class called Account that contains: • Three private instance variables: name (String), account (String), account balance (double). • One constructor, which constructs all instances with the values given. • Getters and setters for all the instance variables. • Debit function that takes an amount from the user and subtract the balance (make sure the balance will not drop below zero after the operation, if this was the case, the function should return false else it should return true)...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose:...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc. __name is a...
Code in C# Part 1 Build a Series class that has the following attributes and behaviors:...
Code in C# Part 1 Build a Series class that has the following attributes and behaviors: Attributes 1.) A name -- this can be any string value (i.e. “Firefly”) 2.) A number of episodes -- this can be any non-negative integer value 3.) An episode length -- this can be a double value, in hours, representing the length of an episode (i.e. 0.5 for a half-hour episode, or 0.37 for a 23 minute episode) 4.) A forKids attribute that holds...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT