Sprint View

2018 FRQ Question 1

4 min read

Question Overview

This question involves simulating a frog attempting to reach a goal distance by hopping. You will implement two methods for the FrogSimulation class.

FrogSimulation Specification

The FrogSimulation class:

  • Instance Variables: goalDistance (target position) and maxHops (maximum hops allowed)
  • hopDistance(): A private method that returns the distance of a single hop (can be positive, negative, or zero)
  • simulate(): Returns true if the frog reaches the goal without going negative; false otherwise
  • runSimulations(int num): Runs num simulations and returns the proportion of successful attempts as a double

Key Insights

  • The frog starts at position 0
  • After each hop, check if position < 0 (fail immediately) or position >= goalDistance (succeed immediately)
  • For runSimulations, use (double) successes / num to avoid integer division

Code Runner Challenge

Implement FrogSimulation

View IPYNB Source
// CODE_RUNNER: Implement FrogSimulation

public class FrogSimulation {
    private int goalDistance;
    private int maxHops;

    public FrogSimulation(int dist, int numHops) {
        goalDistance = dist;
        maxHops = numHops;
    }

    private int hopDistance() {
        return (int) (Math.random() * 11) - 2; // -2 to 8
    }

    public boolean simulate() {
        int position = 0;

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

            if (position < 0) {
                return false;
            }

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

        return false;
    }

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

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

        return (double) successes / num;
    }
}

public class Main {
    public static void main(String[] args) {
        FrogSimulation sim = new FrogSimulation(24, 5);

        System.out.println("Single simulation: " + sim.simulate());
        System.out.println("Proportion successful in 1000 runs: " + sim.runSimulations(1000));
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Example 1: Successful Simulation

Given: goalDistance = 24, maxHops = 5, Hop sequence: 5, 7, -2, 8, 6

Hop Distance Position Negative? Goal? Result
1 5 5 No No Continue
2 7 12 No No Continue
3 -2 10 No No Continue
4 8 18 No No Continue
5 6 24 No Yes (24 >= 24) Return true

What you should see when you run the code:

  • Single simulation: true or false (varies due to randomness)
  • Proportion: approximately 0.3-0.5 (varies)

Example 2: Frog Goes Negative (Failure)

Given: goalDistance = 24, maxHops = 5, Hop sequence: 6, -7, …

Hop Distance Position Negative? Goal? Result
1 6 6 No No Continue
2 -7 -1 Yes N/A Return false

Key Point: The simulation ends immediately when the frog goes negative. Remaining hops are not executed.

Solution Explanation

Here is the complete implementation that satisfies all 9 points on the rubric.

Part (a): simulate() - 5 Points

public boolean simulate() { int position = 0;

for (int i = 0; i < maxHops; i++) {
    position += hopDistance();
    
    if (position < 0) {
        return false;
    }
    
    if (position >= goalDistance) {
        return true;
    }
}

return false; }
  • Initialize position to 0
  • Loop exactly maxHops times
  • Update position with hopDistance()
  • Check if position < 0 and return false immediately
  • Check if position >= goalDistance and return true immediately
  • Return false after loop if goal not reached

Part (b): runSimulations(int num) - 4 Points

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

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

return (double) successes / num; }
  • Initialize counter to 0
  • Loop num times
  • Call simulate() and increment counter if it returns true
  • Use (double) successes / num to avoid integer division

How It Works

  1. simulate() tracks the frog’s position starting from 0, updating after each hop
  2. The method returns immediately when the frog goes negative or reaches the goal
  3. runSimulations(int num) calls simulate() multiple times and calculates the success rate
  4. Casting to double is essential to get a decimal proportion instead of 0

Scoring Guidelines

Points Breakdown (9 points total)

Part (a): simulate() (5 points)

  • 1 point: Initializes position variable to 0
  • 1 point: Loops exactly maxHops times
  • 1 point: Calls hopDistance() and updates position
  • 1 point: Checks if position < 0 inside loop and returns false immediately
  • 1 point: Checks if position >= goalDistance inside loop and returns true; returns false after loop

Part (b): runSimulations(int num) (4 points)

  • 1 point: Initializes counter to 0
  • 1 point: Loops num times
  • 1 point: Calls simulate() and increments counter when true
  • 1 point: Returns (double) successes / num

Common Mistakes to Avoid

  • Checking conditions outside the loop instead of inside
  • Using == instead of >= for goal check
  • Using position <= 0 instead of position < 0
  • Not casting to double: successes / num gives 0 instead of proportion
  • Not returning immediately when conditions are met

Course Timeline