SE1011
Class Exercises

UML Class Diagram

+-----------------------------------------+
|                  Mouse                  |   <== Class name
+-----------------------------------------+
| - daysOld: int                          |
| - weight: double                        |   <== Attributes/object variables
| - growthRate: double                    |
+-----------------------------------------+
| + setGrowthRate(rate: double): void     |
| + grow(): void                          |   <== Behaviors/methods
| + display(): void                       |
+-----------------------------------------+

Exercise 1: Mouse Implementation

Implement the Mouse class shown in the UML class diagram above.

  • The grow() method must increase the age of the mouse by one and adjust the weight of the mouse as dictated by the growth rate. Note: newWeight = oldWeight*growthRate
  • The display() method prints to the console the information associated with the Mouse object using the following format: "8 day old mouse weighing 7.2 ounces"

Exercise 2: Mouse Modifications

Modify the Mouse class as follows. Assume that a mouse object mickey exists:

  1. Make it possible to say mickey.setName("Mickey"); and have the name Mickey display when mickey.display(); is called.
  2. Make it possible to say mickey.grow(3); and have the mouse age by three days (instead of just one as is done with mickey.grow();).
  3. Create an accessor that returns the mouse's current weight.
  4. Create an accessor that returns the mouse's current age (in days).
  5. Create an accessor that returns the mouse's current growth rate.

Exercise 3: Using the Mouse class

Create a driver program that asks the user to enter the name and growth rate for a mouse and displays characteristics for the mouse at the end of each week of growth for its first year of life.

Create a driver program that asks the user to enter an integer, desiredMice and then does the same thing as above, except for desiredMice mouse objects.

Last modified: Wednesday, 12-Oct-2016 09:45:43 EDT