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

Polymorphism Write classes and interfaces to represent the following: Adoptable

ID: 3681481 • Letter: P

Question

Polymorphism

Write classes and interfaces to represent the following:

Adoptable

Amphibian

Animal

Bat

Dog

Fish

Frog

Flyable

Goldfish

Mammal

WaterLiveable

Whale

You need to decide on the structure. Consider:

Which should be an abstract class?

Which should be an interface?

How should the classes be related through inheritance?

In what classes should methods be placed?

What methods should be overridden?

Some additional details/requirements:

All animals have a name.

All classes have a toString method that returns the animal's name.

All animals have a method "isWarmBlooded" that returns a boolean.

Animals that can be adopted as pets have a method "getHomecareInstructions" that return a description of how to care for the animal.

Animals that can live underwater have a method "canLiveOnLand" that returns a boolean of whether the animal can also live on land.

Animals that can fly have a method "getFlightSpeed" that returns the average miles per hour that the animal can fly.

This assignment isn't necessarily difficult from a programming perspective. What you should spend time on instead is carefully considering the design of you classes and how they should be related through inheritance or interfaces. To get full credit:

Your class hierarchy should make sense. (You can assume common knowledge or "googleable" facts about animals- you won't be graded on your biology knowledge!)

Place common code as high up in the hierarchy as possible.

Declare classes that should not be instantiated as abstract.

In abstract classes, methods that must be implemented by all subclasses should be declared abstract.

Remember that classes can only have one parent but can implement multiple interfaces.

I have included a test file you can use to test your code. Note that this file might not contain all possibilities you want to test! You can edit this file to add more tests if necessary.

Submit a zip file containing 12 java files.

Explanation / Answer

AnimalKingdomTester.java

public class AnimalKingdomTester {


   public static void main(String[] args) {

       Animal[] animals = new Animal[5];
       animals[0] = new Dog("Rover");
       animals[1] = new Whale("Beluga");
       animals[2] = new Bat("Dracky", 15);
       animals[3] = new Goldfish("Nemo");
       animals[4] = new Frog("Prince");
      
       for(Animal a : animals) {
           System.out.println(a + " is a " + a.getClass().getSimpleName() + " which " +
                   (a.isWarmBlooded() ? "is" : "is not" ) + " warm blooded.");
          
           if(a instanceof Adoptable)
               System.out.println(" " + ( (Adoptable) a).getHomeCareInstructions());
          
           if(a instanceof Flyable)
               System.out.println(" Flies up to " + ( (Flyable) a).getFlightSpeed() + " mph!");
          
           if(a instanceof WaterLiveable)
               System.out.println(" "+a.getName()+" can live in the water " +
                       (( (WaterLiveable) a).canLiveOnLand() ? " and can also" : "but cannot" ) +
                       " live on land.");
       }
      

   }

}

Adoptable.java

public interface Adoptable
{
   String getHomeCareInstructions();
}

Amphibian.java


public abstract class Amphibian extends Animal implements WaterLiveable{
   public Amphibian(String name){
       super(name);
   }

   @Override
   public boolean isWarmBlooded(){
       return false;
   }

   @Override
   public boolean canLiveOnLand(){
       return true;
   }

}

Animal.java

public abstract class Animal
{
    private final String name;

    public Animal(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public abstract boolean isWarmBlooded();

    @Override
    public String toString()
    {
        return String.format(getName());
    }

}

Bat.java

public class Bat extends Mammal implements Flyable{
  
   int speed;

   public Bat(String name, int speed){
       super(name);
       this.speed = speed;
   }

   @Override
   public int getFlightSpeed(){
       return speed;
   }
}

Dog.java

public class Dog extends Mammal implements Adoptable{
   public Dog(String name){
       super(name);
   }

   @Override
   public String getHomeCareInstructions(){
       String instructions = "Feed dog kibble three times a day and take on walks once a day.";
       return instructions;
   }
}

Fish.java

public class Fish extends Animal implements WaterLiveable{

   public Fish(String name){
       super(name);
   }
  
   @Override
   public boolean isWarmBlooded(){
       return false;
   }
  
   @Override
   public boolean canLiveOnLand(){
       return false;
   }
}

Flyable.java

public interface Flyable
{
   int getFlightSpeed();
}

Frog.java

public class Frog extends Amphibian implements Adoptable{

   public Frog(String name){
       super(name);
   }

   @Override
   public String getHomeCareInstructions(){
       String instructions = "Feed twice a day and change water once a week";
       return instructions;
   }
}

Goldfish.java

public class Goldfish extends Fish implements Adoptable{

   public Goldfish(String name){
       super(name);
   }

   @Override
   public String getHomeCareInstructions(){
       String instructions = "Two pinches of fish food daily and clean take once a week";
       return instructions;
   }

}
mammal.java

public class Mammal extends Animal {
   public Mammal(String name){
       super(name);
   }

   @Override
   public boolean isWarmBlooded(){
       return true;
   }
}

WaterLiveable.java

public interface WaterLiveable
{
   boolean canLiveOnLand();
}

Whale.java

public class Whale extends Mammal implements WaterLiveable{
   public Whale(String name){
       super(name);
   }

   @Override
   public boolean canLiveOnLand(){
       return false;
   }

}