To the Zoo! Open a new Eclipse (or using your favorite Java editor) Java Project
ID: 3857617 • Letter: T
Question
To the Zoo! Open a new Eclipse (or using your favorite Java editor) Java Project Chapter9Exercise and create a class Animal. Give the Animal a single private int attribute hunger to hold how hungry the Animal is, a constructor that takes no arguments and sets this attribute to zero, and a method getHunger that returns it Then write an abstract method talk that takes no arguments and returns nothing. (Recall that to make a method abstract, you put abstract at the beginning of the method signature (before public) and put a semicolon at the end of the signature instead of an opening brace.) Try to compile this. You will get an error message about not having overridden the abstract method talk. This error comes because the class Animal has an abstract method, but it is not declared to be an abstract class. Add abstract between public and class at the top of the file. This tells the computer that you intend Animal to be an abstract class. (Eclipse tries to help you with errors as you write code; pay attention to these hints and messages; don't just simply correct code without understanding what these errors are pointing to!) Now check that Animal compiles. It does, but because Animal is an abstract class, you are not able to actually create Animal objects. For that, we need to create a subclass. Create a class Zebra. Make it a subclass of Animal by adding extends Animal to the end of the line giving the class name. Write a constructor for Zebra that takes no arguments. The entire body of this method should be super() This calls the constructor for Animal. That constructor then sets its attribute hunger to 0. (Actually, Java will automatically call the superclass constructor for us, but it's a good habit to explicitly include a call to a superclass constructor in each subclass constructor.) If you try to compile at this point, you again get the error message about not having overridden the talk method. Oops! By inheriting from Animal, we promised to implement a talk method. Add a method talk that takes no arguments and returns nothing, just as promised in the Animal class. Make this method print the string "The Zebra quietly chews." After writing talk, the project should compile. Create a Zebra object (you can do this by writing a main method either in the Zebra class or in a client class; the latter is preferred as it is not a good practice to add main method to a pure Java class). Even though we did not declare any attributes within the code for Zebra, you see that it has the int attribute inherited from Animal. Note the methods are able to invoke on the Zebra object talk and getHunger. Call both methods to verify that talk prints out the message about chewing and that getHunger returns 0 There wouldn't be any reason to write the abstract class Animal if we planned to write only one subclass. Write another subclass of Animal called Lion. As we did with Zebra, give it a constructor that takes no arguments and calls the Animal constructor. Then write a talk method that just prints the message "Roar!". Verify that this compiles and that both getHunger and talk work as intended in the Lion. So far the hunger attribute does not do very much because it never changes from 0. Add a method timePasses to Animal that increases the hunger attribute by one. (The idea is that this is called each unit of time so the animals gradually get hungrier over time.) Lions are not content to quietly get hungrier, though. Override the timePasses method in Lion with a method that increases hunger by 1 as above, but also prints the message "The Lion paces hungrily." if its new value is at least 3. Note that you will not be able to access hunger directly because it is a private attribute of Animal. One solution is to change the access restrictions on hunger (e.g. make it public), but better is to access the attribute indirectly. To increase hunger, call the the timePasses method of Animal (using super.timePasses():). Then, use the getHunger method to read the value of hungerExplanation / Answer
abstract class Animal //abstract base class
{
private int hunger;
public Animal()
{
hunger = 0;
}
public int getHunger()
{
return hunger;
}
public abstract void talk(); //abstract method
public void timePasses()
{
++hunger;
}
public void feed()
{
hunger = 0;
}
}
class Zebra extends Animal
{
public Zebra()
{
super();
}
public void talk()
{
System.out.println(" The Zebra quietly chews...");
}
public String toString()
{
return " Zebra";
}
}
class Lion extends Animal
{
public Lion()
{
super();
}
public void talk()
{
System.out.println(" Roar!..");
}
public void timePasses()
{
super.timePasses();
if(getHunger() >= 3)
System.out.println(" The Lion paces hungrily.");
}
public String toString()
{
return " Lion";
}
}
class Bear extends Animal
{
public Bear()
{
super();
}
public void talk()
{
System.out.println(" Grunts!..");
}
public void timePasses()
{
super.timePasses();
}
public String toString()
{
return " Bear";
}
}
class Zoo
{
private Animal cage1,cage2,cage3;
public Zoo()
{
cage1 = null;
cage2 = null;
cage3 = null;
}
public void putInCage1(Animal a)
{
cage1 = a;
}
public void putInCage2(Animal a)
{
cage2 = a;
}
public void putInCage3(Animal a)
{
cage3 = a;
}
public void print()
{
System.out.println(" The zoo contains the following : ");
if(cage1 != null) {
System.out.println(" "+cage1);
}
if(cage2 != null) { //print 2nd animal
System.out.println(" "+cage2);
}
if(cage2 != null) { //print 2nd animal
System.out.println(" "+cage3);
}
}
public void timePasses()
{
if(cage1 != null) {
cage1.timePasses();
}
if(cage2 != null) { //print 2nd animal
cage2.timePasses();
}
if(cage2 != null) { //print 2nd animal
cage3.timePasses();
}
}
public void allTalk()
{
if(cage1 != null) {
cage1.talk();
}
if(cage2 != null) {
cage2.talk();
}
if(cage2 != null) {
cage3.talk();
}
}
public void feedAll()
{
if(cage1 != null) {
cage1.feed();
}
if(cage2 != null) {
cage2.feed();
}
if(cage2 != null) {
cage3.feed();
}
}
}
class TestAnimal
{
public static void main (String[] args)
{
Zebra z = new Zebra();
Lion l = new Lion();
Bear b = new Bear();
Zoo zoo = new Zoo();
zoo.putInCage1(l);
zoo.putInCage2(b);
zoo.putInCage3(z);
zoo.print();
zoo.timePasses();
zoo.allTalk();
zoo.feedAll();
}
}
Output:
The zoo contains the following :
Lion
Bear
Zebra
Roar!..
Grunts!..
The Zebra quietly chews...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.