Computer Science A
Course Progress
0/0
OCS Build and Lesson
Code Runner - Java
Code Runner - Examples
Code Runner - JavaScript
FRQ - Methods and Control Structures
Challenge Submission Test
2021 FRQ 3
2023 FRQ 3
2024 FRQ 3
2024 FRQ 2
2024 FRQ 1
2024 FRQ 4
FRQ 2 - Sign Class
2023 FRQ 1
2021 FRQ 2
2019 FRQ 4
2019 FRQ 2
2019 FRQ 1
2016 FRQ 3
2018 FRQ Question 4
2018 FRQ Question 3
2018 FRQ Question 2
2018 FRQ Question 1
2017 FRQ 4
2017 FRQ 3
2017 FRQ Question 2
2017 FRQ 1
2016 FRQ 4
2016 FRQ 2
2016 FRQ Q1
FRQ - 2D Arrays
FRQ - ArrayLists
2025 FRQ 4
2025 FRQ 3
2025 FRQ 2
2025 FRQ 1
FRQ - Classes
FRQ - Array
2023 FRQ 4
2022 FRQ 4
2022 FRQ 3
2022 FRQ 2
2022 FRQ 1
2021 FRQ 4
2021 FRQ 1
2015 FRQ 4
2015 FRQ 2
2015 FRQ 1
2015 FRQ 3
2014 FRQ Q2 - Writing a Class
2019 FRQ 3
2014 FRQ 1
Sprint View
Week 19
2018 FRQ Question 1
2018 FRQ Question 1
4 min read
- Question Overview
- FrogSimulation Specification
- Key Insights
- Code Runner Challenge
- Example 1: Successful Simulation
- Example 2: Frog Goes Negative (Failure)
- Solution Explanation
- Scoring Guidelines
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) andmaxHops(maximum hops allowed) - hopDistance(): A private method that returns the distance of a single hop (can be positive, negative, or zero)
- simulate(): Returns
trueif the frog reaches the goal without going negative;falseotherwise - runSimulations(int num): Runs
numsimulations and returns the proportion of successful attempts as adouble
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 / numto 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
maxHopstimes - Update position with
hopDistance() - Check if position < 0 and return
falseimmediately - Check if position >= goalDistance and return
trueimmediately - Return
falseafter 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
numtimes - Call
simulate()and increment counter if it returnstrue - Use
(double) successes / numto avoid integer division
How It Works
simulate()tracks the frog’s position starting from 0, updating after each hop- The method returns immediately when the frog goes negative or reaches the goal
runSimulations(int num)callssimulate()multiple times and calculates the success rate- Casting to
doubleis 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
maxHopstimes - 1 point: Calls
hopDistance()and updates position - 1 point: Checks if position < 0 inside loop and returns
falseimmediately - 1 point: Checks if position >= goalDistance inside loop and returns
true; returnsfalseafter loop
Part (b): runSimulations(int num) (4 points)
- 1 point: Initializes counter to 0
- 1 point: Loops
numtimes - 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 <= 0instead ofposition < 0 - Not casting to double:
successes / numgives 0 instead of proportion - Not returning immediately when conditions are met