Question

The prompt is essentially a simulation of a frog hopping. The frog can jump back and forth and it is considered a success if it makes it to the goal in the amount of jumps given.

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

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

    private int hopDistance(){}
    public boolean simulate(){}
    public double runSimulations(int num){}
}

Part A

The distance away from the goal is defined as dist while the number of jumps given is defined as numHops so if we call the method FrogSimulation using FrogSimulation sim= new FrogSimulation(24,5);. The simulation would have a goal at a distance of 24 inches and a maximum amount of 5 jumps. If the frog makes it to or past the goal sim.simulate will return true and if it is negative or has just not made it to the goal but is not a negative value then it will return false

Requirements

  • Complete method simulate.
  • Requirement: must use hopDistance
  • Return true if frog successfully reaches or goes past the goal

Solution

First we define location to be at zero, our starting point. Then we use a for loop so that i will continue to increase until it reaches the max number of hops and so that for as long as this happens, the frog will hop (ie. location= location+hopDistance, which means that the distance the frog jumps will be added to location every time the frog jumps in order to have the final location of the frog). After the frog has finished jumping, we used an if loop so that if the frog is at or beyond the goal then it will be true and if it is at a negative value or behind the starting value then it will return false. If the frog has used up all its jumps but has not made it to the goal or at a negative value then it will return false regardless which is done by a return false outside of the for loop.

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

Part B

We can then use the values given by the method simulate in order to finish the simulation of the frog. Prompt B is asking for us to finish the simulation and declare whether or not the frog has won the game ie made it to the goal, which we can find using the boolean values given by simulate, and then find the proportion of wins overall after a certain number of runs.

Requirements

  • Complete runSimulations method to do a certain amount of runs and then output the proportion of simulations where the frog reaches the goal

Solution

This is a method called runSimulations and basically if we call this method such as FrogSimulation run = new FrogSimulation(23); it should run the simulation 23 times and count the number of successes- which will eventually be changed into a decimal of successes/tries. We do this by setting the number of successes to zero to begin with and then we use a for loop, with basically the same idea as part a, so that the code will run for as many times as directed. Then inside of that for loop, we have an if loop so that for as long as simulate runs true then the success counter will increase and the proportion of successes/tries will be returned

public double runSimulations(int num){
    int success=0;
    for(int i=0; i<num; i++ ){
        if(simulate()){
            success++
        }
        return success/num;
    }
}