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 and Explanation

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;
    for (int count = 0; count < MaxHops; count++) 
    {
        position += hopDistance(); // calling hopDistance method 
        if (position >= goalDistance) // distance of multiple hops is at least goal distance
        {
            return true; // goal reached
        }
        else if (position < 0) // distance of multiple hops is less than initial position 
        {
            return false; // goal not reached 
        }
    }
    return false;
}
  • Keep track of position and set starting position as zero
  • We have to track this position through a "for" loop that is initially 0 and go to our maximum number of hops
  • We need to have a code that adds hop distance to our position
  • Things that will stop the code: if we have reached the maximum number of hops OR if the position is = goal distance
  • Using the "if/else if" statements, if the position is greater than or equal to the goal Distance, the code will be true
  • However, if our position is negative, we want to return false
  • If neither true or false occurs, the code will run until we reach maximum number of hops
  • Starting count at 0 and keeps going until frog reaches goal distance (true) or negative position (false) or reaches maximum number of hops (false)

Part B and Explanation

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. Complete method runSimulations below. Assume that simulate works as specified, regardless of what you wrote in part (a). You must use simulate appropriately to receive full credit.

public double runSimulation(int num) // int is the number of times we want to run the loop; called simulate method
{
    int countSuccess = 0;
    for (int i = 0; i < num; i++){  // keeps track of the counts so far
        if (simulate()){
            countSuccess++; // if it's successful, the hops will increase by 1
        }
    }
    return (double) countSuccess / num; // gives a proportion of successes with double arthmetic
}
  • Need to declare a double and start it at zero
  • We need to keep track of runs
  • Simulate is a boolean function that returns true or false
  • If true, then I want to increase the number of successes
  • If false, then the run is a fail
  • At the end, I want to return successes/# of simulations for the frog

Scoring Guidelines

  • Part A:
    • Calls hopDistance and uses returned distance to adjust (or represent) the frog’s position
    • Initializes and accumulates the frog’s position at most maxHops times (must be in context of a loop)
    • Determines if a distance representing multiple hops is at least goalDistance
    • Determines if a distance representing multiple hops is less than starting position
    • Returns true if goal ever reached, false if goal never reached or position ever less than starting position
  • Part B
    • Calls simulate the specified number of times (no bounds errors)
    • Initializes and accumulates a count of true results
    • Calculates proportion of successful simulations using double arithmetic
    • Returns calculated value