Question: In JAVA Create an animal class hierarchy as follows: Write a class named Animal with the following void methods which take no arguments: reproduce: prints

In JAVA

Create an animal class hierarchy as follows:

Write a class named Animal with the following void methods which take no arguments:

  • reproduce: prints Animals reproduce in various ways.
  • breathe: prints Animals breathe in various ways.
  • move: prints Animals move in various ways.

Write a class named Mammal which extends Animal and overrides each method:

  • reproduce: prints Mammals bear live young.
  • breathe: prints Mammals breathe using lungs.
  • move: prints Mammals move on legs.

Write a class named Fish which extends Animal and overrides each method:

  • reproduce: prints Fish lay eggs.
  • breathe: prints Fish breathe through gills.
  • move: prints Fish move by swimming.

Write a class named Dog which extends Mammal and overrides the move method:

  • move: prints Dogs walk on four legs.

Write a class named Kangaroo which extends Mammal and overrides the move method:

  • move: prints Kangaroos hop on two legs.

Create class Zoo that has a factory which create an object of type Animal or one its subclasses based on a string argument. Zoo also contains a static processAnimal method which takes a reference to an Animal as an argument and calls its reproduce, breathe, and move methods.

The main method of the Zoo class uses a loop to prompt the user to enter a string containing the type of animal and uses the factory to return an Animal reference. If the reference is null, an error message is displayed. Otherwise, the processAnimal method is called with the reference as its argument. The user is then prompted to enter another animal or to quit the program.

Sample Run:

Add an animal to the zoo:Tree

Tree is not an animal

Add another animal(Y or N):Y

Add an animal to the zoo:Dog

Mammals breathe using lungs.

Mammals bear live young.

Dogs walk on four legs.

Add another animal(Y or N):Y

Add an animal to the zoo:Fish

Fish breathe through gills.

Fish lay eggs.

Fish move by swimming.

Add another animal(Y or N):N

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!