2025 AP CSA FRQ 1: DogWalker

2025 FRQ #1: DogWalker

Methods and Control Structures

9 Points Total
Jump to: Part A Part B Common Mistakes Related FRQs
Practice Timer (25 min recommended)
25:00

Overview

This question involves a simulation of a dog walker who walks dogs for a dog-walking company throughout the day.

TopicsMethods, Loops, Math.min
Difficulty Medium
Time~25 minutes
TypeMethods

Problem Description

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 totalDogs instance 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

Part A: walkDogs (4 points)

Write the walkDogs method. The method simulates the dog walker walking dogs during the hour specified by the parameter hour.

The method should:

  1. Determine how many dogs are available at the given hour using numAvailableDogs
  2. Walk the minimum of the available dogs and 3 (since the walker can only handle 3 dogs per hour)
  3. Add the number of dogs walked to the instance variable totalDogs
  4. Return the number of dogs walked during this hour

Precondition: 0 <= hour <= 23

Method Signature: public int walkDogs(int hour)

Write Your Solution

Check Your Code

🔓 View Solution & Rubric

Scoring Rubric

Points Criterion
+1 Calls numAvailableDogs(hour)
+1 Uses Math.min to limit to 3 dogs
+1 Updates totalDogs by adding dogs walked
+1 Returns the number of dogs walked this hour

Solution

public int walkDogs(int hour)
{
    int available = numAvailableDogs(hour);
    int walked = Math.min(available, 3);
    totalDogs += walked;
    return walked;
}

Explanation

The solution gets available dogs, uses Math.min to cap at 3, updates the running total, and returns how many were walked this hour.

Part B: dogWalkShift (5 points)

Write the dogWalkShift method. The method simulates the dog walker working a shift from startHour to endHour (inclusive) and returns the total number of dogs walked during the shift.

The method should:

  1. Call walkDogs for each hour from startHour to endHour, inclusive
  2. Accumulate the total number of dogs walked across all hours in the shift
  3. Return the total for the shift (not the cumulative totalDogs)

Example: If the walker works from hour 9 to hour 12 and walks 3, 2, 0, and 3 dogs respectively, dogWalkShift(9, 12) should return 8.

Precondition: 0 <= startHour <= endHour <= 23

Important: You must use walkDogs appropriately to receive full credit. Assume walkDogs works as specified regardless of what you wrote in part (a).
Method Signature: public int dogWalkShift(int startHour, int endHour)

Write Your Solution

Check Your Code

🔓 View Solution & Rubric

Scoring Rubric

Points Criterion
+1 Loops from startHour to endHour
+1 Loop bounds are correct (inclusive)
+1 Calls walkDogs for each hour
+1 Accumulates total from walkDogs returns
+1 Returns the shift total

Solution

public int dogWalkShift(int startHour, int endHour)
{
    int shiftTotal = 0;
    for (int hour = startHour; hour <= endHour; hour++)
    {
        shiftTotal += walkDogs(hour);
    }
    return shiftTotal;
}

Explanation

Loop from start to end (inclusive with <=), call walkDogs each iteration, sum the returns, and return the shift total.

⚠️ Common Mistakes to Avoid

Off-by-one error

Use <= in loop condition since endHour is inclusive

Not using helper method

Must call walkDogs, not numAvailableDogs directly in part (b)

Returning totalDogs instead of shift total

Part (b) returns only the dogs walked during THIS shift

Related Practice: Methods and Control Structures

Similar FRQs:

Study Guide:

Daily Practice:

Contact form