Question: Using the code sent to you, ensure that it is appropriately utilizing inheritance and polymorphism. If necessary, modify the code to establish a clear inheritance

Using the code sent to you, ensure that it is appropriately utilizing inheritance and polymorphism. If necessary, modify the code to establish a clear inheritance hierarchy and demonstrate polymorphic behavior. Then, design and implement a way to incorporate recursion and Exception handling. You must handle all possible errors appropriately. I will try to break your code. Use recursion by creating a makeSound() method that calls a recursive method called recursiveSound(int repetitions). The recursiveSound() method should print the sound the animal makes the specified number of times.
Animal class: public abstract class Animal {
public String type;
public String sound;
Animal(String sound, String type){
this.sound = sound;
this.type = type;
}
public abstract void setType(String type);
public abstract String getType();
public abstract void setSound(String sound);
public abstract String getSound();
public String toString(){
return getType()+""+ getSound();
}
}
animal driver: public class AnimalDriver {
public static void main(String[] args){
Mammal pig = new Mammal("oink","pig");
System.out.println("A "+pig.getType()+" goes "+pig.getSound());
Mammal dog = new Mammal("woof","dog");
System.out.println("A "+dog.getType()+" goes "+dog.getSound());
Mammal cat = new Mammal("meow","cat");
System.out.println("A "+cat.getType()+" goes "+cat.getSound());
}
mammal class:public class Mammal extends Animal {
public Mammal(String type, String sound){
super(type,sound);
}
@Override
public void setType(String type){
this.type = type;//
}
@Override
public String getType(){
return type;
}
@Override
public void setSound(String sound){
this.sound = sound;
}
@Override
public String getSound(){
return sound;
}Ublic String getSu perString(){
return super.toString();
}
} sing the code sent to you, ensure that it is appropriately utilizing inheritance and polymorphism. If necessary, modify the code to establish a clear inheritance hierarchy and demonstrate polymorphic behavior. Then, design and implement a way to incorporate recursion and Exception handling. You must handle all possible errors appropriately. I will try to break your code. Use recursion by creating a makeSound() method that calls a recursive method called recursiveSound(int repetitions). The recursiveSound() method should print the sound the animal makes the specified number of times.
Animal class: public abstract class Animal {
public String type;
public String sound;
Animal(String sound, String type){
this.sound = sound;
this.type = type;
}
public abstract void setType(String type);
public abstract String getType();
public abstract void setSound(String sound);
public abstract String getSound();
public String toString(){
return getType()+""+ getSound();
}
}
animal driver: public class AnimalDriver {
public static void main(String[] args){
Mammal pig = new Mammal("oink","pig");
System.out.println("A "+pig.getType()+" goes "+pig.getSound());
Mammal dog = new Mammal("woof","dog");
System.out.println("A "+dog.getType()+" goes "+dog.getSound());
Mammal cat = new Mammal("meow","cat");
System.out.println("A "+cat.getType()+" goes "+cat.getSound());
}
mammal class:public class Mammal extends Animal {
public Mammal(String type, String sound){
super(type,sound);
}
@Override
public void setType(String type){
this.type = type;//
}
@Override
public String getType(){
return type;
}
@Override
public void setSound(String sound){
this.sound = sound;
}
@Override
public String getSound(){
return sound;
}
I have everything mostly done. I need to figure out the recursive method and the exception handling.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!