Question
I need help with an assignment. It's an online class so when I run into problems I can't just ask. If you can help me
I need help with an assignment. It's an online class so when I run into problems I can't just ask. If you can help me get on the right path and understand what I'm doing that would be great. I understand the concept of inheritance, it is more the coding aspect that I am missinterpreting. This is the assignment:
CSC 143 Weekly Exercisei This exercise involves building a number of classes to become more comfortable with inheritance and theassociated concepts.
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 takesno arguments and sets this attribute to zero, and a method getHunger that returns it. Then write an abstractmethod talk that takes no arguments and returns nothing. (Recall that to make a method abstract, you putabstract at the beginning of the method signature (before public) and put a semicolon at the end of the signatureinstead of an opening brace.) Try to compile this. You will get an error message about not having overridden the abstract method talk. Thiserror comes because the class Animal has an abstract method, but it is not declared to be an abstract class. Addabstract between public and class at the top of the file. This tells the computer that you intend Animal to be anabstract class. (Eclipse tries to help you with errors as you write code; pay attention to these hints andmessages; dont 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 actuallycreate 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 classname. 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 willautomatically call the superclass constructor for us, but its a good habit to explicitly include a call to asuperclass 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 noarguments and returns nothing, just as promised in the Animal class. Make this method print the string TheZebra quietly chews.. After writing talk, the project should compile. Create a Zebra object (you can do this by writing a main methodeither in the Zebra class or in a client class; the latter is preferred as it is not a good practice to add main methodto a pure Java class). Even though we did not declare any attributes within the code for Zebra, you see that ithas 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 wouldnt be any reason to write the abstract class Animal if we planned to write only one subclass. Writeanother subclass of Animal called Lion. As we did with Zebra, give it a constructor that takes no arguments andcalls the Animal constructor. Then write a talk method that just prints the message Roar!. Verify that thiscompiles 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 toAnimal that increases the hunger attribute by one. (The idea is that this is called each unit of time so the animalsgradually 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 paceshungrily. if its new value is at least 3. Note that you will not be able to access hunger directly because it is aprivate attribute of Animal. One solution is to change the access restrictions on hunger (e.g. make it public), butbetter is to access the attribute indirectly. To increase hunger, call the the timePasses method of Animal (usingsuper.timePasses();). Then, use the getHunger method to read the value of hunger.
Now that the animals can get hungry, we should have a way to feed them. Add a method feed to Animal that setshunger back to 0. Compile and create some animals to make sure they get hungrier and that hungry lions startpacing. As a last step for the animals, lets write a toString method. This is the method that is called automatically whenan object is printed (or a String representation is needed for some other reason). It must take no arguments andreturn a String. The classes already inherit such a method from the Object class (which you can verify by lookingin inherited from Object in the menu of methods). Since this toString method doesnt give a very useful string,lets override this method in Zebra and Lion with methods that return the class of the animal. (So the method inZebra returns Zebra and the one in Lion returns Lion.) Because the signature of your toString method needsto exactly match the signature of the one youre overriding, you will need to explicitly make it public: public String toString() Once you complete this method, again compile, create some animals, and ensure that this works beforeproceeding. Now lets write some code to use our animal classes. Create a new class called Zoo. This one should not inheritfrom Animal since it doesnt have an is a relationship with Animal. To start with, give your Zoo a single attributecalled cage which stores an Animal. Give it a constructor that takes an Animal object and stores it in thisattribute. Then write a method print that prints the message The zoo contains a followed the animals type.(Since weve written a toString method, you can print an Animal object as if it were a String.) Printing the Zoo should produce a message such as the following:The zoo contains a Lion A zoo with only one animal isnt going to attract many visitors. Therefore, we want to expand the Zoo class sothat it can accommodate multiple Animal objects. Rename cage to cage1 and add cage2 and cage3. Remove theargument to the constructor and just have it set all these variables to null. (That means that the variable doesntreference any object.) Then create methods to set each of these (call them putInCage1, putInCage2, andputInCage3); the methods take an Animal object and set the appropriate variable. Then modify print to print anyof these that are not null in a format such as The zoo contains the following:LionZebra Since you dont want to print a null reference, youll need to check each of them before printing it: if(cage1 != null) {//print first animal} if(cage2 != null) {//print 2nd animal } ... Now, write Zoo methods timePasses, allTalk, and feedAll that call the corresponding method for each (non-null)animal in the zoo. Then, add another subclass of Animal; youll find this quite easy since the only methods youhave to write are the constructor, talk, and toString. To submit this weekly exercise, gather all *.java source-code files that have been created for the above classes(in Eclipse, you can find these files in src folder under the folder that has been created for this Java project),place them in a folder, zip/compress and attach the folder.
My code so far:
package Animal;
public abstract class Animal { private int hunger; Animal(){ hunger = 0; } int gethunger(){ return hunger; } abstract public void talk(); } abstract class Zebra extends Animal{ super(); public void talk(){ System.out.println("The Zebra Quietly Chews"); } }
The super part is underlined in red for an identifier but I don't know what I should do about that and I just need help with the assignment in general. Also do you know what my teacher means by compile it? Do I need to go to the run thing on my computer and use javac?
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