Question: This question explores subclassing, abstract classes and interfaces. You should be able to answer this question once you have completed Unit 6. In this question
This question explores subclassing, abstract classes and interfaces. You should be able to answer this question once you have completed Unit 6.
In this question you will write a class Dog. The scenario is that of pets. We have an abstract superclass Pet (which you are provided with) together with a number of concrete subclasses, Dog and Fish and its subclass PondFish.
Several of the methods that you will write for this class are interdependent. It is therefore important that you test your code thoroughly after completion of each part. Some test code is contained in the README.TXT file for this project. You do not need to include evidence of your testing in your Solution Document.
Although you should answer the parts of this question in order, you will find it useful to read through the whole question quickly, before starting, to get an overview of how the methods work together.
Remember that when writing methods, you should reuse existing methods wherever possible.
- a.Create Dog as a subclass of Pet and add the initial comment TMA02 Q3 for the new class, together with your name and date, following the outline provided by BlueJ. Delete any unwanted template code generated by BlueJ.
(1 mark)
- b.Instances of the Dog class should have two private instance variables of type int, called happiness and energyLevel. Write declarations for these instance variables and associated getter methods. Note that there are no setter methods for these variables. Their values are set by other means.
(2 marks)
- c.Write a two-argument constructor public Dog(String aName, String aDescription) so that it initialises its inherited instance variables as for its superclass and assigns the value 2 to happiness.
(2 marks)
- d.Both happiness and energyLevel can have their values decremented by 1 (down to a minimum of 0) using the helper methods decrementHappiness() and decrementEnergyLevel(). Similarly these instance variables can have their values incremented by 1, with no upper limit on the values, using the helper methods incrementHappiness() and incrementEnergyLevel(). Write these four helper methods.
(3 marks)
-
e.
- i.Write a public instance method called walkies() which takes no argument and returns no value. The method should repeatedly (as long as energyLevel is above 0) print out "I'm going for a walk!", increase happiness by 1, then print out "I'm getting hungry" and decrement energyLevel by 1.
(4 marks)
- ii.Write a public instance method called sleep() which takes no argument and returns a boolean value. The method should return true if happiness is above 0 and energyLevel is above 0. If happiness is 0 the method should print out "Not happy, can't sleep" and return false. If energyLevel is 0 the method should print out "Hungry, can't sleep" and return false.
(4 marks)
- iii.Write a public instance method called noWalkies() which takes no argument and returns no value. The method should print out "No walkies :-(" and decrement happiness (down to a minium of 0).
(2 marks)
- i.Write a public instance method called walkies() which takes no argument and returns no value. The method should repeatedly (as long as energyLevel is above 0) print out "I'm going for a walk!", increase happiness by 1, then print out "I'm getting hungry" and decrement energyLevel by 1.
- f.It is now required that instances of Dog, along with instances of Fish and PondFish, two other provided classes unrelated to Dog, implement lovable behaviour, each in its own way. Dog, Fish and PondFish implement the Lovable interface which specifies three methods: stroke() which takes no argument and returns no value, canStroke() which takes no argument and returns true or false, and feed() which takes no argument and returns no value.
- i.Write the Lovable interface.
(2 mark)
- ii.Modify the headers of the Dog, Fish and PondFish classes so that they declare their intention to implement the Lovable interface.
Note: In order for your project to compile you will need to include the following stub implementations for the above three methods in the Dog class:
/** * stub for stroke() method, returns no value */ public void stroke() { } /** * stub for canStroke() method, returns false */ public boolean canStroke() { return false; } /** * stub for feed() method, returns no value */ public void feed() { }
(1 mark)
- iii.Look carefully at the code in the supplied PondFish class which is a subclass of the Fish class. Why do versions of the three methods of the Lovable interface not have to be written in the PondFish class?
- i.Write the Lovable interface.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
