AP Exam FRQ Practice

FRQ Practice #1 (2018 FRQ 1)

Orignal Problem/Given Code

public class FrogSimulation
{
    /** Distance, in inches, from the starting position to the goal. */
    private int goalDistance;
    /** Maximum number of hops allowed to reach the goal. */
    private int maxHops;
    /** Constructs a FrogSimulation where dist is the distance, in inches, from the starting
    * position to the goal, and numHops is the maximum number of hops allowed to reach the goal.
    * Precondition: dist > 0; numHops > 0
    */
    public FrogSimulation(int dist, int numHops)
{
    goalDistance = dist;
    maxHops = numHops;
}
    /** Returns an integer representing the distance, in inches, to be moved when the frog hops.
     */
    private int hopDistance()
    { /* implementation not shown */ }
    /** Simulates a frog attempting to reach the goal as described in part (a).
    *  Returns true if the frog successfully reached or passed the goal during the simulation;
    * false otherwise.
    */
    public boolean simulate()
    { /* to be implemented in part (a) */ }
    /** Runs num simulations and returns the proportion of simulations in which the frog
    * successfully reached or passed the goal.
    * Precondition: num > 0
    */
    public double runSimulations(int num)
    { /* to be implemented in part (b) */ }
}

Part A

Instructions: Write the simulate method, which simulates the frog attempting to hop in a straight line to a goal from the frog’s starting position of 0 within a maximum number of hops. The method returns true if the frog successfully reached the goal within the maximum number of hops; otherwise, the method returns false.

public boolean simulate()
{
    int position = 0;  // intializes starting position as 0
    for (int count = 0; count < maxHops; count++)  // cycles through the method as long as the count is less than the maxHops --> increments the hops
    {
        position += hopDistance();   // will get our position and see how far the location is
        if (position >= goalDistance) // if our position is greater that the goal distance, the output is true
        {
            return true;
        }
        else if (position < 0) // if position is negative return the boolean as false
        {
            return false; 
        }
    }
    return false;  // if our position is less than the goal distance, the output is false
}

Explanation with Pictures

frog1

frog2

frog3

frog4

Part B

Instructions: Write the runSimulations method, which performs a given number of simulations and returns the proportion of simulations in which the frog successfully reached or passed the goal. For example, if the parameter passed to runSimulations is 400, and 100 of the 400 simulate method calls returned true, then the runSimulations method should return 0.25.

public double runSimulations(int num) // the number of times we want to run the loop
{
    int countSuccess = 0;  // initializes the count to start at 0
    for (int count = 0; count < num; count++) // loops through the code --> increments 
    {
        if(simulate()) // if the simulation is successful, it will increase the countSuccess
        {
            countSuccess++;
        }
    }
    return (double)countSuccess / num; // gives the proportion of the success to create the correct output
}

Explanation with Pictures

bfrog1

bfrog2

bfrog3