This question involves a simulation of a dog walker who walks dogs for a dog-walking company. The DogWalker class provides methods to simulate walking dogs during specific hours and across a work shift.
A DogWalker object represents an individual dog walker. The dog walker keeps track of the total number of dogs they have walked. The dog walker can walk a maximum of 3 dogs per hour.
Class Information
public class DogWalker
{
private int totalDogs;
/** Returns the number of dogs available at the given hour.
* Precondition: 0 <= hour <= 23
*/
private int numAvailableDogs(int hour)
{ /* implementation not shown */ }
/** Walks dogs during the specified hour and returns the number walked.
* Precondition: 0 <= hour <= 23
*/
public int walkDogs(int hour)
{ /* to be implemented in part (a) */ }
/** Returns the total dogs walked during a shift from startHour to endHour.
* Precondition: 0 <= startHour <= endHour <= 23
*/
public int dogWalkShift(int startHour, int endHour)
{ /* to be implemented in part (b) */ }
}
How Dog Walking Works
- The helper method
numAvailableDogs(hour)returns the number of dogs available to be walked at a given hour. - The dog walker will walk as many dogs as available, but never more than 3 in a single hour.
- Each dog walked should be added to the
totalDogsinstance variable.
Example
Assume a DogWalker object walker has been created with totalDogs = 0.
| Method Call | numAvailableDogs returns | Dogs Walked | totalDogs after | Return Value |
|---|---|---|---|---|
walker.walkDogs(9) |
5 | 3 (max) | 3 | 3 |
walker.walkDogs(10) |
2 | 2 | 5 | 2 |
walker.walkDogs(11) |
0 | 0 | 5 | 0 |
walker.walkDogs(12) |
4 | 3 (max) | 8 | 3 |