Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with question 1(b). This is a Java coding question. Thank you. 1. This question involves reasoning about a simulation of a frog

Please help me with question 1(b). This is a Java coding question. Thank you.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

1. 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) { 1 to be implemented in part (b) */ } } (a) 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. The Frogsimulation class provides a method called hopDistance that returns an integer representing the distance (positive or negative) to be moved when the frog hops. A positive distance represents a move toward the goal. A negative distance represents a move away from the goal. The returned distance may vary from call to call. Each time the frog hops, its position is adjusted by the value returned by a call to the hopDistance method. The frog hops until one of the following conditions becomes true: The frog has reached or passed the goal. The frog has reached a negative position The frog has taken the maximum number of hops without reaching the goal. The following example shows a declaration of a FrogSimulation object for which the goal distance is 24 inches and the maximum number of hops is 5. The table shows some possible outcomes of calling the simulate method. Frogsimulation sim = new Frogsimulation (24, 5); Values returned by hopDistance() 7, -2, 8, 6 7. 6. 6 Final position of frog 24 Return value of sim. simulate() true true 5. 6 25 Example Example 2 Example 3 Example 4 Example 5 6, -6, 31 31 true 2, -8 -2 false 5. 2. 3 18 false Class information for this question public class Frogsimulation private int goalDistance private int maxHops private int hopDistance() public boolean simulate() public double runSimulations (int num) 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() (b) 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 retum 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. 1** 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) ./

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions