Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Objective In this exercise, you will create a hierarchy of animals that is

ID: 3575534 • Letter: J

Question

JAVA

Objective

In this exercise, you will create a hierarchy of animals that is rooted in an abstract class Animal. Several of the animal classes will implement an interface called Pet. You will experiment with variations of these animals, their methods, and polymorphism.

Experiment by:

A) calling the methods in each object,

b) casting objects,

c) using polymorphism, and

d) using super to call super class methods

Assignment

1.Create the Animal class, which is the abstract superclass of all animals.

1. Declare a protected integer attribute called legs, which records the number of legs for this animal.

2 Define a protected constructor that initializes the legs attribute.

3 Declare an abstract method eat.

4 Declare a concrete method walk that prints out something about how the animals walks (include the number of legs).

2.Create the Spider class.

1 The Spider class extends the Animal class.

2 Define a default constructor that calls the superclass constructor to specify that all spiders have eight legs.

3 Implement the eat method.

3. Create the Pet interface specified by the UML diagram.

4. Create the Cat class that extends Animal and implements Pet.

1 This class must include a String attribute to store the name of the pet.

2 Define a constructor that takes one String parameter that specifies the cat's name. This constructor must also call the superclass constructor to specify that all cats have four legs.

3 Define another constructor that takes no parameters. Have this constructor call the previous constructor (using the this keyword) and pass an empty string as the argument.

4 Implement the Pet interface methods.

5 Implement the eat method.

5.Create the Fish class. Override the Animal methods to specify that fish can't walk and don't have legs.

6.Create an TestAnimalsprogram. Have the mainmethod create and manipulate instances of the classes you created above. Start with:

Fish d = new Fish();

Cat c = new Cat("Fluffy");

Animal a = new Fish();

Animal e = new Spider();

Pet p = new Cat();

Instructions:

Develop and test the classes as described in the Assignment section above.

Your lab assignment should have only one Java file, namely; TestAnimals.java file that has all classes and interfaces you’ve implemented.

Explanation / Answer

abstract class Animal{
   protected int legs;

   protected Animal(int legs) {
       super();
       this.legs = legs;
   }
  
   public abstract void eat();
  
   public void walk(){
       System.out.println("I walk with "+ legs + " legs.");
   }
  
}
class Spider extends Animal{

   public Spider() {
       super(8);
   }

   @Override
   public void eat() {
       System.out.println("I am a Spider and I am eating currently. So call me later.");
      
   }
  
}
interface Pet{
   //TODO please add the abstract methods
}

class Cat extends Animal implements Pet{
   String name;

   public Cat(String name) {
       super(4);
       this.name = name;
       // TODO Auto-generated constructor stub
   }
  
   public Cat(){
       this("");      
   }

   public void setPetName(String name) {
       this.name = name;
      
   }

   public String getPetName() {
       // TODO Auto-generated method stub
       return null;
   }

   @Override
   public void eat() {
       System.out.println("I am a Cat with name "+name+". I am eating..");
      
   }
  
}

class Fish extends Animal{

   protected Fish() {
       super(0);
       // TODO Auto-generated constructor stub
   }

   @Override
   public void eat() {
       System.out.println("I am a Fish. I don't have any legs.");      
   }
  
}
public class TestAnimals{
  
   public static void main(String args[]){
      
       //Add the methods in Pet Interface which ever you want to add
       Fish d = new Fish();
       Cat c = new Cat("Fluffy");
       Animal a = new Fish();
       Animal e = new Spider();
       Pet p = new Cat();
      
       c.eat();
       d.eat();
       a.eat();
       e.eat();
   }
}