Question

In: Computer Science

Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named...

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

Please write your complete Animal class, Cat class and a driver class as required below

a (35 pts) Write your complete Java code for the Animal class in Animal.java file

b (30 pts) Write your complete Java code for the Cat class in Cat.java file

b (35 pts) Write your test Java class and its main method which will create two Cats instances: e1 and e2, e1 is created with the default constructor, e2 is created with the explicit value constructor. Then update e1 to reset its vegetarian, eatings, numOfLegs and color. Output both cats’ detailed information. The above test Java class should be written in a Java file named testAnimal.java.

Solutions

Expert Solution

class Animal
{
  
protected boolean vegetarian;
protected String eatings;
protected int numOfLegs;

public Animal()
{
   vegetarian = false;
   eatings = "meat";
   numOfLegs = 4;
}

public 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;
  
}


public boolean getVegetarian()
{
   return vegetarian;
}
public String getEatings()
{
   return eatings;
}
public int getNumOfLegs()
{
   return numOfLegs;
}

public String toString()
{
return "Animal’s vegetarian : "+ vegetarian +", eatings : "+ eatings+" number of legs : "+numOfLegs;
}

}


class Cat extends Animal
{

private String color;
public Cat()
{
   super();
}

public Cat(boolean vegetarian,String eatings, int numOfLegs, String color)
{
super(vegetarian,eatings,numOfLegs);
this.color = color;
}


public void SetColor(String color)
{
   this.color = color;
}

public String GetColor()
{
   return color;
}

public String toString()
{
   return super.toString() +" color : "+color;
}

}
class testAnimal
{
   public static void main (String[] args)
   {
     
       Cat e1 = new Cat();
       Cat e2 = new Cat(false,"meat,milk",4, "white");
      
       e1.SetAnimal(true,"grass",4);

e1.SetColor("black");
      
       System.out.println(e1);
       System.out.println(e2);
      
   }
}

Output:

Animal’s vegetarian : true, eatings : grass   number of legs : 4  color : black
Animal’s vegetarian : false, eatings : meat,milk   number of legs : 4  color : white

Do ask if any doubt. Please up-vote.


Related Solutions

In java: -Create a class named Animal
In java: -Create a class named Animal
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
In java, design a class named Motorcycle that has the following fields: • year – The...
In java, design a class named Motorcycle that has the following fields: • year – The year field is an int that holds the motorcycle’s year • make – The make field references a String object that holds the make of the motorcycle. • speed – The speed field is an int that holds the motorcycle’s current speed The class should have the following constructor and other methods: • Constructor – the constructor should accept the motorcycle’s year and make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT