Zoo Abstract Inheritance Lab Objective: Students should be able to declare an ab
ID: 3805385 • Letter: Z
Question
Zoo Abstract Inheritance Lab
Objective: Students should be able to declare an abstract class. Students should be able to create more subclasses for the class. They should be able to create an array of class objects. They should be able to write a tester program to display all the functionality of their program.
It’s good programming style to keep the amount of code to a minimum and to reuse existing code as much as possible. Code that is economical is easy to maintain. The concept of inheritance does just that: it illustrates a way to make code both economical and extendable.
When creating a model using inheritance, you start by defining a more general class with common instance variables and methods. The more specific classes are told to “extend” the general class. The general class then becomes the superclass and the others become the subclasses. The subclasses automatically inherit the instance variables and methods from the superclass.
The result is called the inheritance hierarchy; a simple such hierarchy is shown here. In this case, the Animal class is the superclass. It has two subclasses, Cow and Camel. (This doesn’t work in reverse. A subclass can extend only one class.)
Let’s model this animal hierarchy.
Write an Animal Class. This will be the superclass.
This class should be declared as abstract because we don’t want to be able to create an instance of this class.
This class will have 2 attributes: a name and a sound.
Write a constructor that takes 2 parameters: name and sound.
Write accessor methods only for each attribute.
Write a toString method that prints out the attributes in the form: Spot says woof.
Write a class called Cow. A cow is an animal.
This class will not have any additional instance variables.
Define a constructor that has a name and sound as a parameter.
All methods will be inherited. You do not need to write any additional methods.
Write a class called Camel.
A camel will add an instance variable for the number of humps.
Provide a constructor that accepts 3 parameters: name, sound, and number of humps.
Write an accessor method to get the number of humps.
Override the toString method to display a message in the format: The camel bertha says spit and has 2 humps
Define your own subclass. Your subclass should be unique so be creative.
Write a class called ZooTester.java. Create an array of 5 animals and print out each animal. Be sure to test any additional methods you write.
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
###################### Animal.java ###################
public abstract class Animal {
// instance variables
private String name;
private String sound;
// constructor
public Animal(String name, String sound) {
this.name = name;
this.sound = sound;
}
public String getName() {
return name;
}
public String getSound() {
return sound;
}
@Override
public String toString() {
return name+" says "+sound;
}
}
###################### Cow.java ###################
public class Cow extends Animal{
// constructor
public Cow(String name, String sound) {
super(name, sound);
}
}
###################### Camel.java ###################
public class Camel extends Animal{
private int number_of_humps;
// constructor
public Camel(String name, String sound, int number_of_humps) {
super(name, sound);
this.number_of_humps = number_of_humps;
}
public int getNumber_of_humps() {
return number_of_humps;
}
@Override
public String toString() {
return "The camel "+getName()+" says "+getSound()+" and has "+number_of_humps+" humps";
}
}
###################### ZooTester.java ###################
public class ZooTester {
public static void main(String[] args) {
// Creating Cow and Camel objects
Cow c1 = new Cow("Goli", "waff");
Cow c2 = new Cow("Spot", "bhaye");
Cow c3 = new Cow("Sundri", "woof");
Camel ca1 = new Camel("Badki", "ouuuw", 2);
Camel ca2 = new Camel("bertha", "spit", 2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(ca1);
System.out.println(ca2);
System.out.println("Number of humps : "+ca2.getNumber_of_humps());
}
}
/*
Sample run:
Goli says waff
Spot says bhaye
Sundri says woof
The camel Badki says ouuuw and has 2 humps
The camel bertha says spit and has 2 humps
Number of humps : 2
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.