Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

THIS IS WHAT I HAVE.

public class Animal {

public Animal(String name) {

this.name = name;

}

public void reproduce() {

return name + " reproduce in various ways.";

}

public void breathe() {

return name + " Breathe in various ways.";

}

public void move() {

return name + " Move in various ways.";

}

}

class Mammal extends Animal {

public Mammal(String name)

this.name = name;

@Override public void reproduce() {

return name + " Mammals bear live young.";

}

@Override public void breathe() {

return name + " Mammals breathe using lungs.";

}

@Override public void move() {

return name + " Mammals move on legs.";

}

}

class Fish extends Animal {

public Fish(String name)

this.name = name;

@Override public void reproduce() {

return name + " Fish lay eggs.";

}

@Override public void breathe() {

return name + " Fish breathe through gills.";

}

@Override public void move() {

return name + " Fish move by swimming.";

}

}

class Dog extends Mammal {

public Dog(String name)

this.name = name;

@Override public void move() {

return name + " Dogs walk on four legs.";

}

}

class Kangaroo extends Mammal {

public Kangaroo(String name)

this.name = name;

@Override public void move() {

return name + " Kangaroos hop on two legs.";

}

}

Main method

public class Zoo {

Animal object = null;

if(s.equals("Dog"))

{

object = new Dog();

}

else if(s.equal("Kangaroo"))

{

object = new Kangaroo();

}

else if(s.equal("Fish"))

{

object = new Fish();

}

public processAnimal

return object;

public static void main(String() args){

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago