Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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.

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.

Also, although several of the methods that you write could in practice be private, for the purposes of this question you should make them public.

Remember that when writing methods, you should reuse existing methods wherever possible.

Note that, while answering this question, there will be times when a class or classes wont compile because youve not yet written some code.

This project contains an implementation of the Pet class. Read through this class now, noting the instance variables, constructor and supplied getter and setter methods.

  • a.Create Dog as a subclass of Pet and add the initial comment for the new class, together with your name and date.
  • 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.
  • 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.
  • 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.
    • 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.

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

    • 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).

      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.

      • 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() { }

      • 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?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

Explain why input controls are so important. Discuss fully.

Answered: 1 week ago