FrogSimulation FRQ Notes

AP Computer Science A — How to Solve the Frog Simulation FRQ

Overview

This FRQ asks you to complete two methods in the FrogSimulation class: simulate() and runSimulations(int num).

The class represents a frog trying to hop from position 0 to a goal distance. The frog has a limited number of hops. Each hop may move the frog forward or backward.

Big Idea: The frog succeeds if it reaches or passes the goal before running out of hops or landing on a negative position.

1. Understand the Class

The class is called:

public class FrogSimulation

It has two important instance variables:

private int goalDistance;
private int maxHops;
Variable Meaning
goalDistance How far the frog must travel to reach the goal.
maxHops The maximum number of hops the frog is allowed to take.

The frog always starts at position 0.

2. Understand hopDistance()

The class already has this method:

private int hopDistance()

You do not write this method. It returns an integer that tells how far the frog moves on one hop.

Example:

position += hopDistance();
Starting Position hopDistance() Returns New Position
10 5 15
10 -7 3

Part A — Writing simulate()

3. What simulate() Must Do

The method header is:

public boolean simulate()

This method should simulate one attempt by the frog to reach the goal.

Return Value Meaning
true The frog reached or passed the goal.
false The frog failed to reach the goal.

Stopping Conditions

The frog keeps hopping until one of these happens:

  1. The frog reaches or passes the goal.
  2. The frog reaches a negative position.
  3. The frog uses the maximum number of hops without reaching the goal.

4. Key Variables Needed

Inside simulate(), you need to track the frog's current position:

int position = 0;

You also need a loop to count the number of hops:

for (int hop = 0; hop < maxHops; hop++)
This loop makes sure the frog does not hop more than the allowed number of times.

5. How to Think Through the Algorithm

The frog starts at position 0.

Each time through the loop:

  1. Call hopDistance().
  2. Add the result to the frog's current position.
  3. Check whether the frog reached or passed the goal.
  4. Check whether the frog went below 0.

The frog succeeds if:

position >= goalDistance

The frog fails immediately if:

position < 0

If the loop finishes and the frog never reached the goal, return false.

6. Part A Solution

public boolean simulate()
{
    int position = 0;

    for (int hop = 0; hop < maxHops; hop++)
    {
        position += hopDistance();

        if (position >= goalDistance)
        {
            return true;
        }

        if (position < 0)
        {
            return false;
        }
    }

    return false;
}

7. Why This Works

The variable position starts at 0 because the frog begins at the starting position.

The loop runs at most maxHops times because the frog only gets a limited number of hops.

After each hop, the code checks for success first:

if (position >= goalDistance)

This means the frog reached or passed the goal.

Then it checks for failure:

if (position < 0)

This means the frog moved behind the starting point and the simulation should stop.

If neither condition happens and the frog uses all hops, the method returns false.

Part B — Writing runSimulations(int num)

8. What runSimulations() Must Do

The method header is:

public double runSimulations(int num)

This method runs the simulate() method multiple times. It returns the proportion of simulations that were successful.

Example: If runSimulations(400) is called and 100 simulations are successful, the method should return 0.25.
100 / 400 = 0.25

9. Important Idea: Proportion

A proportion is:

number of successes / total number of attempts

For this FRQ:

successful simulations / num

Because the method returns a double, you must avoid integer division.

Incorrect:
return successCount / num;

If both variables are integers, Java performs integer division.

Correct:
return (double) successCount / num;

10. Key Variables Needed

You need a counter to track successful simulations:

int successCount = 0;

You need a loop to run the simulation num times:

for (int i = 0; i < num; i++)

Each time through the loop, call:

simulate()

If simulate() returns true, add 1 to successCount.

11. Part B Solution

public double runSimulations(int num)
{
    int successCount = 0;

    for (int i = 0; i < num; i++)
    {
        if (simulate())
        {
            successCount++;
        }
    }

    return (double) successCount / num;
}

12. Why This Works

The method runs exactly num simulations.

Each time simulate() returns true, the code increases the success counter.

At the end, the method divides the number of successful simulations by the total number of simulations.

The cast to double is important:

(double) successCount / num

This makes sure Java returns a decimal answer instead of using integer division.

Complete Student-Friendly Solution

public boolean simulate()
{
    int position = 0;

    for (int hop = 0; hop < maxHops; hop++)
    {
        position += hopDistance();

        if (position >= goalDistance)
        {
            return true;
        }

        if (position < 0)
        {
            return false;
        }
    }

    return false;
}

public double runSimulations(int num)
{
    int successCount = 0;

    for (int i = 0; i < num; i++)
    {
        if (simulate())
        {
            successCount++;
        }
    }

    return (double) successCount / num;
}

Common Mistakes Students Make

Mistake 1: Forgetting to Call hopDistance()

The frog's movement comes from hopDistance().

Incorrect:

position += 1;

Correct:

position += hopDistance();

Mistake 2: Using > Instead of >=

The frog succeeds if it reaches or passes the goal.

Incorrect:

if (position > goalDistance)

Correct:

if (position >= goalDistance)

Mistake 3: Forgetting to Stop When Position Is Negative

The frog stops if it reaches a negative position.

if (position < 0)
{
    return false;
}

Mistake 4: Running Too Many Hops

The frog cannot hop forever. The loop must stop after maxHops.

for (int hop = 0; hop < maxHops; hop++)

Mistake 5: Integer Division in Part B

Incorrect:

return successCount / num;

Correct:

return (double) successCount / num;

AP Exam Strategy

For this FRQ, students should look for these clues:

FRQ Phrase What It Means in Code
Maximum number of hops Use a loop controlled by maxHops.
Reached or passed the goal Check position >= goalDistance.
Negative position Check position < 0.
Runs num simulations Use a loop that runs num times.
Proportion Divide successes by total attempts.
Returns true if successful Use an if statement with simulate().

Big Idea Summary

The simulate() method performs one frog attempt.

The runSimulations() method performs many frog attempts and calculates how often the frog succeeds.

The main skills tested are:

Final Thought: This FRQ is a good example of how AP Computer Science A tests whether students can carefully translate written instructions into Java code.