FRQ

This question involves reasoning about a simulation of a frog hopping in a straight line. The frog attempts to hop to a goal within a specified number of hops. The simulation is encapsulated in the following FrogSimulation class. You will write two of the methods in this class.

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) */ 
    } -->
}

Complete method simulate below. You must use hopDistance appropriately to receive full credit.

/** 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()

public boolean simulate(){
    int h = 0;
    for(int i=0; i < numHops; i++){
        int h += hopDistance();
        if(h >= goalDistance){
            return true;
        }
        else if(h < 0){
            return false;
        }
    }
    
    return false;
}

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. /** 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)

public double runSimulations(int num){
    double value = 0;
    for(int i=0; i > num; i++){
        boolean number = simulate();
        if(number == true){
            value += 1;
        }
    }
    value = value/num;
    return value
}