Objective: The purpose of this assignment is to give you additional experience i
ID: 3551550 • Letter: O
Question
Objective:
The purpose of this assignment is to give you additional experience in working with classes, objects, and inheritance. In particular, the classes you write will make use of polymorphism (meaning "many structures"), which gives a subclass the ability to change a behavior it inherits from its superclass.
Animal Sounds and Behaviors
Implement a set of classes for the Animal Kingdom that will demonstrate polymorphism. A base Animal class will have generic methods makeSound, move, getFood, and eatFood. An Animal will also have an instance variable for its name and a static instance variable for its type. Implement a describe method by calling each of the generic methods in an order of your choice. Implement at least the Mammal and Reptile subclasses ofAnimal, at least two subclasses of Mammal, and at least two subclasses of Reptile. In each of these subclasses you will implement all the generic methods from the higher-level classes by using print statements describing what would happen for that method applied to that particular animal. For example, the makeSound method in a Dog class might print "Bark! Bark!," while the getFood method might print "begs at the dinner table."
You will need a 1-variable constructor that has a String parameter for the name of the Animal being constructed.
In a TestAnimal class, declare several animal objects that refer to the various subclasses. Calling the describe method for each animal will show that each behaves in its own way, appropriate to that type of animal. A subclass may override the describe method if the order of method calls defined in the Animal class is not appropriate for that subclass.
For example,
would result in the following being printed:
In the TestAnimal class, also define an array variable named family that consists of 5 members from two of your subclasses (such as 3 Dog animals and 2 Reptile animals), each with a different name, such as DogA, DogB, etc. Using a loop, call the describe method for each of the five.
Explanation / Answer
import java.util.ArrayList;
public class Animal {
String name;
static String type;
public Animal(String name) {
super();
this.name = name;
}
public Animal() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getType() {
return type;
}
public static void setType(String type) {
Animal.type = type;
}
public void describe()
{
}
}
class Dog extends Animal
{
public Dog(String name) {
super(name);
Dog.setType("Dog");
// TODO Auto-generated constructor stub
}
public void makeSound()
{
System.out.print(" Bark! Bark!..");
}
public void move()
{
System.out.println(" four legs");
}
public void getFood()
{
System.out.println("begs at the dinner table");
}
public void eatFood()
{
System.out.println("chewing");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getType() {
return type;
}
public static void setType(String type) {
Animal.type = type;
}
public void describe()
{
System.out. println (getName()+ " is a "+ getType());
System.out. println(getName()+ " Says ");
this.makeSound();
System.out. println(getName()+ " Walks on ");
this.move();
System.out. println(getName()+ " gets food by ");
this.getFood();
System.out. println(getName()+ " eats food by ");
this.eatFood();
}
}
class Reptile extends Animal
{
public Reptile(String name) {
super(name);
Reptile.setType("Reptile");
// TODO Auto-generated constructor stub
}
public void makeSound()
{
System.out.println(" sounds..");
}
public void move()
{
System.out.println(" three legs");
}
public void getFood()
{
System.out.println("fighting");
}
public void eatFood()
{
System.out.println("Eats");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getType() {
return type;
}
public static void setType(String type) {
Animal.type = type;
}
public void describe()
{
System.out. println (getName()+ " is a "+ getType());
System.out. println(getName()+ " Says ");
this.makeSound();
System.out. println(getName()+ " Walks on ");
this.move();
System.out. println(getName()+ " gets food by ");
this.getFood();
System.out. println(getName()+ " eats food by ");
this.eatFood();
}
}
class TestAnimal
{
public static void main(String args[])
{
Dog DogA=new Dog("Rover");
Dog DogB=new Dog("Rover1");
Dog DogC=new Dog("Rover2");
Reptile ReptileA=new Reptile("Reptile1");
Reptile ReptileB=new Reptile("Reptile2");
ArrayList<Animal> arr=new ArrayList<Animal>();
arr.add(DogA);
arr.add(DogB);
arr.add(DogC);
arr.add(ReptileA);
arr.add(ReptileB);
Animal a=new Animal();
for(int i=0;i<arr.size();i++)
{
a=arr.get(i);
a.describe();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.