Question
1) In the Animal class,add an input String parameter to the constructor called type, which we will use to identify the type of animal. Add
1) In the Animal class,add an input String parameter to the constructor called type, which we will use to identify the type of animal. Add the appropriate private member variable to copy the input parameters value into and a public accessor method called getType().
2.) In each of the Dog, Cat, Bird, Fish, and Fox classes, add the corresponding type of the animal to the call to the super class constructor. To be super clear by type, I mean Dog for the Dog class, etc...
3.) Back in the Animal class, make it an abstract class and make each of the following methods abstract: numberOfLegs(), numberOfWings(), and breatheUnderwater().
4.) In the Tester class:
a. Remove all the code in the main method aside from the creation of the five animal objects.
b. Create and initialize a variable of type ArrayList.
c. Add each of the five animals into the ArrayList.
d. Use the enhanced for loop syntax to iterate over the ArrayList and call the PrintDetails method for each animal in the ArrayList.
e. Create a method with the following signature:
public static void PrintDetails( Animal a )
That method should use the public methods available on the Animal class to determine if the animal it was passed has legs, wings or gills and in each case call numberOfLegs(), numberOfWings(), or breatheUnderwater() as appropriate. Also call getSoundMade(). You will need to use the getType()method we added in order to produce the sample output below exactly.
_______Code is below:_______________________________________________________________________________________________________________________________________________
package animaldemo;
public class AnimalDemo {
public static void main(String[] args) { Fox fox=new Fox(); Cat cat=new Cat(); Fish fish=new Fish(); Bird bird=new Bird(); Dog dog=new Dog(); try{ System.out.println("Dogs have "+dog.numberOfLegs()+" legs."); System.out.println("Cats have "+cat.numberOfLegs()+" legs."); System.out.println("Birds have "+bird.numberOfLegs()+" legs."); System.out.println("Birds have "+bird.numberOfWings()+" wings."); System.out.print("Fish "); fish.breatheUnderwater(); System.out.println("Foxes have "+bird.numberOfLegs() +"legs."); System.out.println("Attempting things we shouldn't... "); dog.numberOfWings(); dog.breatheUnderwater(); cat.numberOfWings(); cat.breatheUnderwater(); fish.breatheUnderwater(); fish.numberOfLegs(); dog.numberOfWings(); fox.numberOfWings(); fox.breatheUnderwater(); System.out.println("Dog goes "+dog.getSoundMade()); System.out.println("Cat goes "+cat.getSoundMade()); System.out.println("Bird goes "+bird.getSoundMade()); System.out.println("Fish goes "+fish.getSoundMade()); System.out.println("What does the fox say? "+fox.getSoundMade()); } catch(Exception e) { System.out.println(e.getMessage()); } } }
/////////////////////////////////////////////////////////////////// package animaldemo;
//Animal class public abstract class Animal { //TODO: private member variables private String soundMade; private boolean Legs,Wings,Gills; public Animal( String soundMade, boolean hasLegs, boolean hasWings, boolean hasGills ) { //copy the input parameters to private member variables this.soundMade=soundMade; this.Legs=hasLegs; this.Wings=hasWings; this.Gills=hasGills; } //return the value of the corresponding private member variable public String getSoundMade(){ return this.soundMade; } //return the value of the corresponding private member variable public boolean hasLegs() { return this.Legs; } //return the value of the corresponding private member variable public boolean hasWings() { return this.Wings; } //return the value of the corresponding private member variable public boolean hasGills() { return this.Gills; } //throw a RuntimeException indicating that we don't know abstract int numberOfLegs(); //throw a RuntimeException indicating that we don't know abstract int numberOfWings(); //throw a RuntimeException indicating that we don't know if the animal can abstract void breatheUnderwater(); } //////////////////////////////////////////////////////////////
package animaldemo;
public class Fox extends Animal{ Fox() { super("Ring-ding-ding-ding-dingeringeding",true,false,false); } @Override int numberOfLegs() { return 4; }
@Override int numberOfWings() { throw new RuntimeException("Foxes don't have wings!"); }
@Override void breatheUnderwater() { throw new RuntimeException("Fox cant breathe underwater!"); }
}
/////////////////////////////////////////////////////////////////////////
package animaldemo;
public class Dog extends Animal{ Dog() { super("woof",true,false,false); } @Override int numberOfLegs() { return 4; }
@Override int numberOfWings() { throw new RuntimeException("Dog doesnt have wings!"); }
@Override void breatheUnderwater() { throw new RuntimeException("Dog cant breathe underwater!"); }
}
////////////////////////////////////////////////////////
package animaldemo;
public class Cat extends Animal{ Cat() { super("meow",true,false,false); } @Override int numberOfLegs() { return 4; }
@Override int numberOfWings() { throw new RuntimeException("Cat doesnt have wings!"); }
@Override void breatheUnderwater() { throw new RuntimeException("Cat cant breathe underwater!"); }
}
/////////////////////////////////////////////////////////////////////////////
package animaldemo;
public class Fish extends Animal{ Fish() { super("Blub",false,false,true); } @Override int numberOfLegs() { throw new RuntimeException("Fish doesnt have legs!"); }
@Override int numberOfWings() { throw new RuntimeException("Fish doesnt have wings!"); }
@Override void breatheUnderwater() { }
}
///////////////////////////////////////////////////////////
package animaldemo;
public class Bird extends Animal{ Bird() { super("tweet",true,true,false); } @Override int numberOfLegs() { return 2; }
@Override int numberOfWings() { return 2; }
@Override void breatheUnderwater() { throw new RuntimeException("Birds cant breathe underwater!"); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started