Sprint View

2024 FRQ 1

6 min read

2024 FRQ Question 1

This question simulates birds or possibly a bear eating at a bird feeder. The following Feeder classcontains information about how much food is in the bird feeder and simulates how much food is eaten. You will write two methods of the Feeder class.

// CODE_RUNNER: <challenge text> public class Feeder { /**

  • The amount of food, in grams, currently in the bird feeder; initialized in the constructor and
  • always greater than or equal to zero */ private int currentFood; /**
  • Simulates one day with numBirds birds or possibly a bear at the bird feeder,
  • as described in part (a)
  • Precondition: numBirds > 0 / public void simulateOneDay(int numBirds) { / to be implemented in part (a) */ } /**
  • Returns the number of days birds or a bear found food to eat at the feeder in this simulation,
  • as described in part (b)
  • Preconditions: numBirds > 0, numDays > 0 / public int simulateManyDays(int numBirds, int numDays) { / to be implemented in part (b) */ } // There may be instance variables, constructors, or methods that are not shown. }

Part A

simulateOneDay(numBirds) Specification

Write the simulateOneDay method, which simulates one day at a bird feeder. The method models either:

  • numBirds birds visiting the feeder, or
  • (rarely) a bear visiting the feeder.

It must determine how much food is consumed that day and update the instance variable:

  • currentFood (grams of food currently in the feeder)

Daily Conditions

Each day is either normal or abnormal:

  • Normal conditions (95% of the time):
    • Only birds visit the feeder.
    • Every bird eats the same amount of food that day.
    • The amount eaten per bird is a random integer from 10 to 50 grams inclusive, where each integer has an equal probability.
      • Possible values: 10, 11, ..., 49, 50
    • Total food consumed by birds:
      • totalConsumed = numBirds * gramsPerBird
    • If totalConsumed is greater than currentFood, the birds empty the feeder and:
      • currentFood becomes 0.
  • Abnormal conditions (5% of the time):
    • A bear visits and empties the feeder.
    • currentFood becomes 0.

Examples of Possible Outcomes

  • Example 1: If the feeder starts with 500 grams, calling simulateOneDay(12) might result in
    12 birds eating 20 grams each (total 240), leaving 260 grams.

  • Example 2: If the feeder starts with 1000 grams, calling simulateOneDay(22) might result in
    a bear emptying the feeder, leaving 0 grams.

  • Example 3: If the feeder starts with 100 grams, calling simulateOneDay(5) might result in
    5 birds attempting to eat 30 grams each (total 150), but since 150 > 100, the feeder is emptied, leaving 0 grams.

Part A Guidelines

Complete the simulateOneDay method.

Walk Through:

Part (a) — simulateOneDay (cleanly formatted explanation)

Simulate one day at the bird feeder.

1) Bear check (5% chance)

  • Generate a random integer from 0–99.
  • If it is 0–4, a bear shows up: currentFood = 0 (day ends)

2) Otherwise, birds eat

  • Each bird eats the same random amount: 10–50 grams (inclusive) gramsPerBird = (int)(Math.random() * 41) + 10
  • Total food birds want to eat: totalEaten = gramsPerBird * numBirds

3) Update currentFood

  • If totalEaten > currentFood: currentFood = 0
  • Else: currentFood -= totalEaten

// CODE_RUNNER: <challenge text>

Code Runner Challenge

BirdProblemA

View IPYNB Source
// CODE_RUNNER: BirdProblemA

public class Feeder
{
    // Instance variable
    private int currentFood;

    /**
     * Simulates one day with numBirds birds or possibly a bear at the bird feeder,
     * as described in part (a)
     * Precondition: numBirds > 0
     */
    public void simulateOneDay(int numBirds)
    {
        double chance = Math.random();

        // 5% chance a bear visits
        if (chance < 0.05)
        {
            currentFood = 0;
        }
        else
        {
            int gramsPerBird = (int)(Math.random() * 41) + 10; // 10–50 inclusive
            int totalConsumed = numBirds * gramsPerBird;

            if (totalConsumed >= currentFood)
            {
                currentFood = 0;
            }
            else
            {
                currentFood -= totalConsumed;
            }
        }
    }

    /* There may be instance variables, constructors, and methods that are not shown. */
}



public class Main
{
    public static void main(String[] args)
    {
        Feeder feeder = new Feeder();
        feeder.currentFood = 500;

        System.out.println("Starting food: " + feeder.currentFood);

        feeder.simulateOneDay(12);

        System.out.println("Food after one day: " + feeder.currentFood);
    }
}

Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part B

simulateManyDays(numBirds, numDays) Specification

Write the simulateManyDays method. This method uses simulateOneDay to simulate numBirds birds (or possibly a bear) visiting the feeder over at most numDays consecutive days.

The simulation stops when either:

  • numDays days have been simulated, or
  • the feeder becomes empty (no food remains), so no future visitors can find food.

The method returns:

  • the number of days on which birds (or a bear) found food at the feeder
    (i.e., the feeder contained more than 0 grams at the start of that day).

Notes / Intended Behavior

  • Each simulated day should call simulateOneDay(numBirds) only if there is still food.
  • If currentFood becomes 0, the simulation ends early.
  • Count only days where visitors arrived and there was food available when they arrived.

Examples of Possible Outcomes

Part B Guidelines

simulateManyDays(numBirds, numDays) Task

Complete the simulateManyDays method.

Assume that simulateOneDay works as intended, regardless of what you wrote in part (a). You must use simulateOneDay appropriately in order to receive full credit.


What simulateManyDays Must Do

The method should use simulateOneDay(numBirds) to simulate up to numDays consecutive days at the feeder.

The method returns:

  • the number of days that birds (or a bear) found food at the feeder.

Key Requirements

  • Simulate days one at a time, calling simulateOneDay(numBirds) as part of each day’s simulation.
  • Stop simulating early if the feeder becomes empty (currentFood == 0), since no one can find food after that.
  • Only count a day if there is food available at the start of that day (i.e., visitors can “find food”).

Part B Guidelines

Walk Through:

Part (b) — simulateManyDays (cleanly formatted explanation)

Simulate multiple days at the bird feeder, up to numDays total, but stop early if the feeder becomes empty.

Algorithm: 1) Create a counter daysWithFood = 0.

2) Repeat for up to numDays days:

  • If currentFood <= 0: break (stop the simulation)
  • Otherwise: call simulateOneDay(numBirds) increment daysWithFood by 1

3) Return daysWithFood. This is the number of days where the feeder still had food at the start of the day (so the day could be simulated).

// CODE_RUNNER: <challenge text>

Code Runner Challenge

BirdProblemB

View IPYNB Source
// CODE_RUNNER: BirdProblemB

/**
 * Returns the number of days birds or a bear found food to eat at the feeder
 * in this simulation, as described in part (b)
 * Preconditions: numBirds > 0, numDays > 0
 */
public class Feeder
{
    int currentFood;  // instance variable

    // Minimal simulateOneDay so code compiles and runs
    public void simulateOneDay(int numBirds)
    {
        int foodEaten = numBirds * 10;
        currentFood = Math.max(0, currentFood - foodEaten);
    }

    public int simulateManyDays(int numBirds, int numDays)
    {
        int daysWithFood = 0;

        for (int day = 0; day < numDays; day++)
        {
            if (currentFood == 0)
            {
                break;
            }

            daysWithFood++;
            simulateOneDay(numBirds);
        }

        return daysWithFood;
    }

    /* There may be instance variables, constructors, and methods that are not shown. */
}



public class Main
{
    public static void main(String[] args)
    {
        Feeder feeder = new Feeder();
        feeder.currentFood = 50;   // starting food

        int result = feeder.simulateManyDays(2, 10);

        System.out.println(result); // Expected output: 3
    }
}

Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Graidng Guidelines:

Course Timeline