2024 AP CSA FRQ 1 Solution – Feeder (Methods & Simulation)
2024 AP CSA FRQ 1 – Feeder Simulation (Solution)
This solution shows one full-credit way to implement simulateOneDay and simulateManyDays in the Feeder class. The focus is on correct random behavior, updating currentFood, and using the helper method correctly.
Part (a) – simulateOneDay
Goal: Simulate a single day at the feeder with numBirds birds or possibly a bear.
- There is a 5% chance of a bear: the feeder is emptied.
- Otherwise (95% of the time), each bird eats the same random amount between 10 and 50 grams inclusive.
- If birds want more food than is available, they simply empty the feeder.
Random pattern: To get an integer from 10 to 50 inclusive, use
(int)(Math.random() * 41) + 10 because there are 41 possible values (50 − 10 + 1).public void simulateOneDay(int numBirds)
{
// 5% chance that a bear comes and empties the feeder
double r = Math.random();
if (r < 0.05)
{
currentFood = 0;
}
else
{
// Normal conditions: each bird eats between 10 and 50 grams (inclusive)
int gramsPerBird = (int) (Math.random() * 41) + 10;
int totalEaten = gramsPerBird * numBirds;
if (totalEaten >= currentFood)
{
// Birds eat all remaining food
currentFood = 0;
}
else
{
// Some food remains
currentFood -= totalEaten;
}
}
}
Why this earns full credit:
- Correct 5% vs 95% branching based on
Math.random(). - Correct integer range for bird consumption (10–50 inclusive).
- Correct update logic for
currentFood, including emptying the feeder when demand exceeds supply. - No modification of parameters or use of disallowed library methods.
Part (b) – simulateManyDays
Goal: Simulate up to numDays days, each with numBirds birds or a bear, and return the number of days on which food was actually found at the feeder.
- Each simulated day with food uses
simulateOneDay. - If the feeder ever reaches 0 grams, the simulation stops early (later days cannot have food).
- If
currentFoodis 0 at the start, the method returns 0 (no day has food).
public int simulateManyDays(int numBirds, int numDays)
{
int daysWithFood = 0;
int day = 0;
// Continue while we still have days left and there is food to be found
while (day < numDays && currentFood > 0)
{
simulateOneDay(numBirds);
daysWithFood++;
day++;
}
return daysWithFood;
}
Why this earns full credit:
- Uses
simulateOneDayexactly as specified (no reimplementation of its logic). - Stops early when
currentFoodbecomes 0, matching the examples. - Counts only days on which there was food at the start of the day.
- Returns the correct count in all of the sample scenarios from the prompt.
Common mistakes: using an incorrect random range (e.g., 10–49), forgetting the 5% bear case, or calling
simulateOneDay even after currentFood is 0 in part (b).