Help with Java HW. Thanks! Polymorphism Write classes and interfaces to represen
ID: 3591446 • Letter: H
Question
Help with Java HW. Thanks!
Polymorphism
Write classes and interfaces to represent the following:
Adoptable
Amphibian
Animal
Bat
Blue Whale
Cat
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
//Definitions of all classes are follows
//Adoptable.java
public interface Adoptable
{
//method
public String getHomecareInstructions();
}
------------------------------------------------------------------------
//Amphibian.java
public interface Amphibian
{
//method
public String layEggs();
}
------------------------------------------------------------------------
//Mammal.java
public interface Mammal
{
public String breath();
}
------------------------------------------------------------------------
//WaterLiveable.java
public interface WaterLiveable
{
//method
public boolean canLiveOnLand();
}
------------------------------------------------------------------------
//Abstract class
//Animal.java
public abstract class Animal
{
public String name;
//abstract methods for toString and isWarmBlooded
public abstract String toString();
public abstract boolean isWarmBlooded();
public String getName()
{
return name;
}
}
------------------------------------------------------------------------
public class Dog extends Animal implements Mammal,Adoptable
{
public Dog(String name)
{
this.name=name;
}
@Override
public String toString()
{
return "Dog Name: "+getName()+" Warmblood : "+isWarmBlooded()+
getHomecareInstructions()+" "+breath();
}
@Override
public boolean isWarmBlooded()
{
return true;
}
@Override
public String getHomecareInstructions() {
// TODO Auto-generated method stub
return null;
}
@Override
public String breath() {
return "Breath through lungs";
}
}
------------------------------------------------------------------------
//Frog.java
//The frog class inherits from Animal class
//and implments the interfaces Amphibian,WaterLiveable
public class Frog extends Animal implements Amphibian,WaterLiveable
{
//constructor of frog class
public Frog(String name)
{
this.name=name;
}
//ToString retturns the string repersetnation of frog class object
@Override
public String toString()
{
return "Frog : Name : "+getName()+" Warmblood : "+isWarmBlooded()+
" "+layEggs()+"Live on Land : "+canLiveOnLand();
}
//Returns false boolea value
@Override
public boolean isWarmBlooded()
{
return false;
}
@Override
public String layEggs() {
return "Lay eggs";
}
@Override
public boolean canLiveOnLand() {
// TODO Auto-generated method stub
return true;
}
}
------------------------------------------------------------------------
//Goldfish.java
//The class Goldfish that inherits the class Animal
//and implements the classes Adoptable
public class Goldfish extends Animal implements Adoptable
{
public Goldfish(String name) {
this.name=name;
}
@Override
public String toString()
{
return "Fish : Name : "+getName()+" Warmblood : "+isWarmBlooded()+
" "+getHomecareInstructions();
}
@Override
public boolean isWarmBlooded()
{
return false;
}
//Returns the tips for goldfish
@Override
public String getHomecareInstructions()
{
String fishCareTips;
fishCareTips="1.Use Aquarium with fresh fresh water. " +
"2.Provide food in the mornig " +
"3.Clean the aquarium every 15 days";
return fishCareTips;
}
}
------------------------------------------------------------------------
//Whale.java
//The class Whale inherits from Animal class
//and implements the interface classes Mammal,WaterLiveable
public class Whale extends Animal implements Mammal,WaterLiveable
{
public Whale(String name)
{
this.name=name;
}
@Override
public String toString()
{
return "Whale Fish : Name : "+getName()+" Warmblood : "+isWarmBlooded()+
" "+breath()+" Live On Land : "+canLiveOnLand();
}
@Override
public boolean isWarmBlooded()
{
return false;
}
@Override
public String breath() {
return "Breath through lungs";
}
@Override
public boolean canLiveOnLand() {
// TODO Auto-generated method stub
return false;
}
}
------------------------------------------------------------------------
//Driver program to test the classees ,interfaces
/**The driver program that test the classes
* Adoptable,Amphibian,Animal,Dog,Frog,Goldfish,Mammal,WaterLiveable,Whale
* Create an instance of each concrete classes of Dog,Frog, GoldFish
* and Whale and print the details of the classes using toStirng
* method.
* */
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//Create an instance of Dog class using abstact Animal class
Animal dog=new Dog("Puppy");
System.out.println(dog.toString());
System.out.println();
//Create an instance of Frog class using abstact Animal class
Animal frog=new Frog("frogyy");
System.out.println(frog.toString());
System.out.println();
//Create an instance of GoldFish class using abstact Animal class
Animal fish=new Goldfish("Fish");
System.out.println(fish.toString());
System.out.println();
//Create an instance of Whale class using abstact Animal class
Animal whalefish=new Whale("whale fish");
System.out.println(whalefish.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.