Question: 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

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.

a.Create Dog as a subclass of Pet

b.Instances of the Dog class should have two additional private instance variables of type int, called happiness and timesFedToday. 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.

c.

i.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.

ii.Why is it not necessary to set timesFedToday to 0 explicitly in this constructor? Explain your answer.

d.Both happiness and timesFedToday can have their values decremented by 1 (down to a minimum of 0) using the helper methods decrementHappiness()and decrementTimesFedToday(). Similarly these instance variables can have their values incremented by 1, with no upper limit on the values, using the helper methods incrementHappiness() and incrementTimesFedToday(). Write these four helper methods.

e.

i.Write a public instance method called walkies()which takes no argument and returns no value. The method should repeatedly (as long as timesFedToday is above 0) print out Im going for a walk!, increase happiness by 1, then print out Im getting hungry and decrement timesFedToday by 1.

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 timesFedToday is above 0. If either of these values is 0 then print out either or both of Not happy, cant sleep or Hungry, cant sleep as appropriate and return false.

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.

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 PondFishimplement 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.

Note that we are not asking you to actually write these methods for the Dog class, only to show your understanding of Java interfaces by answering the questions below.

i.Modify the headers of the Dog, Fish and PondFish classes so that they declare their intention to implement the Lovable interface.

ii.Write the Lovable interface.

iii.Look carefully at the code in the supplied PondFish class which is a subclass of the Fishclass. Why do versions of the three methods of the Lovable interface not have to be written in the PondFish class?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!