- FrogSimulation.java
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.
Complete method simulate
in this exercise. You must use hopDistance
appropriately to receive full credit.
First attempt to complete this exercise in the free response to practice writing code without the ability to run it. When you think you’ve completed the task, copy your code from here and test it in the following activity.
Upload an image
Upload from your computer
Or paste a link here
Slides and Notes
No slides available for this video
About
Java (main)
Java Version 1.8.0_222