AP CSA FRQ (2025 - Question 1) Dog Walker Problem
2025 AP® Computer Science A Free-Response Questions
Question 1: DogWalkCompany and DogWalker
Context
This question involves dog walkers who are paid through a dog-walking company to take dogs for one-hour walks. A dog-walking company has a varying number of dogs that need to be walked during each hour of the day. A dog-walking company is represented by the following DogWalkCompany class.
public class DogWalkCompany
{
/**
* Returns the number of dogs, always greater than 0, that are available
* for a walk during the time specified by hour
* Precondition: 0 <= hour <= 23
*/
public int numAvailableDogs(int hour)
{ /* implementation not shown */ }
/**
* Decreases, by numberDogsWalked, the number of dogs available for a walk
* during the time specified by hour
* Preconditions: 0 <= hour <= 23
* numberDogsWalked > 0
*/
public void updateDogs(int hour, int numberDogsWalked)
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods that are not shown. */
}
A dog walker is associated with a dog-walking company and is represented by the DogWalker class. You will write two methods of the DogWalker class.
public class DogWalker
{
/** The maximum number of dogs this walker can walk simultaneously per hour */
private int maxDogs;
/** The dog-walking company this dog walker is associated with */
private DogWalkCompany company;
/**
* Assigns max to maxDogs and comp to company
* Precondition: max > 0
*/
public DogWalker(int max, DogWalkCompany comp)
{ /* implementation not shown */ }
/**
* Takes at least one dog for a walk during the time specified by hour, as described in part (a)
* Preconditions: 0 <= hour <= 23
* maxDogs > 0
*/
public int walkDogs(int hour)
{ /* to be implemented in part (a) */ }
/**
* Performs an entire dog-walking shift and returns the amount earned, in dollars, as described in part (b)
* Preconditions: 0 <= startHour <= endHour <= 23
* maxDogs > 0
*/
public int dogWalkShift(int startHour, int endHour)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods that are not shown. */
}
Part A: walkDogs Method
Write the walkDogs method, which updates and returns the number of dogs this dog walker walks during the time specified by hour. Values of hour range from 0 to 23, inclusive.
A helper method, numAvailableDogs, has been provided in the DogWalkCompany class. The method returns the number of dogs available to be taken for a walk at a given hour. The dog walker will always walk as many dogs as the dog-walking company has available to walk, as long as the available number of dogs is not greater than the maximum number of dogs that the dog walker can handle, represented by the value of maxDogs.
Another helper method, updateDogs, has also been provided. So that multiple dog walkers do not sign up to walk the same dogs, the walkDogs method should use updateDogs to update the dog-walking company with the number of dogs this dog walker will walk during the given hour.
Examples:
- If the company has 10 dogs at the given hour but the walker’s maximum is 4,
updateDogsshould indicate that 4 dogs are walked. - If the company has 3 dogs at the given hour and the walker’s maximum is 4,
updateDogsshould indicate all 3 dogs are walked.
The method should return the number of dogs walked during the specified hour. You must use numAvailableDogs and updateDogs appropriately.
Part B: dogWalkShift Method
Write the dogWalkShift method, which performs a dog-walking shift of the hours in the range startHour to endHour, inclusive, and returns the total amount earned.
For each hour:
- Base pay is $5 per dog walked
- Bonus of $3 if at least one of the following is true:
-
maxDogsdogs are walked - The walk occurs between the peak hours of 9 and 17, inclusive
-
Example Calculation:
| Hour | Maximum Number of Dogs | Number of Dogs Walked | Amount Earned ($) |
|---|---|---|---|
| 7 | 3 | 3 | 3 × 5 + 3 = 18 |
| 8 | 3 | 2 | 2 × 5 = 10 |
| 9 | 3 | 2 | 2 × 5 + 3 = 13 |
| 10 | 3 | 3 | 3 × 5 + 3 = 18 |
| Total | 59 | ||
Assume walkDogs works as specified, regardless of what you wrote in part (a). You must use walkDogs appropriately to earn full credit.
Student Code Submission Form
Paste your code into the box below and submit it to Mr. Crow for review/feedback.