60,937 views
24 votes
24 votes
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. TheFrogSimulationclass provides a method calledhopDistancethat 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 thehopDistance method. g

User Sathyz
by
3.1k points

1 Answer

30 votes
30 votes

Answer:

Step-by-step explanation:

The following code is written in Python. It creates the simulate method which takes in an argument for the max number of hops and the goal distance. Then it loops the number for the max number of hops and asks the user for a hop input using the hopDistance() method (which was not included so it was made, this can be removed if wanted). Then it updates position. If after the max hops goal was reached it returns True, otherwise it returns False.

def simulate(max, goal):

position = 0

print("Goal is " + str(goal))

for x in range(max):

hop = hopDistance()

position += int(hop)

if position >= goal:

return True

else:

return False

def hopDistance():

return input("Frog Hop Distance this hop: ")

print(simulate(4, 20))

Write the simulate method, which simulates the frog attempting to hop in a straight-example-1
User Nbrew
by
3.0k points