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
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after...
Create java class with name Dog. Instructions for Dog class:              This class is modeled after a Dog. You should have instance variables as follows: The Dog’s name The number of tricks known by the Dog. The color*of the Dog’s coat (fur). *: For the Dog’s fur color you should use the Color class from the java.awt package. You should choose proper types and meaningful identifiers for each of these instance variables. Your class should contain two constructor methods. A...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of...
Haviing trouble with my homework (java) 1)Write the class CustomerAccount, which has fields/attributes called custID of type String, custName of type String and checkingBalance of type double and savingBalance of type double 2)Write the constructor for CustomerAccount class. The constructor takes parameters to initialize custID, custName, checkingBalance and savingBalance. 3)Write the mutator method for all attributes of the class CustomerAccount. then Write the toString method for class CustomerAccount.
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:
Challenge: Animal Class Description: Create a class in Python 3 named Animal that has specified attributes...
Challenge: Animal Class 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....
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
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...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...