Question

Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...

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

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...