FrogSimulation

For this FRQ you are implementing two methods inside a FrogSimulation class. You are also given a method named hopDistance() that returns a random integer for how for a frog hopped.

Part A

Given a maximum number of hops you’re checking whether the frog is able to get to or past the goalDistance. simulate should return true if the frog reaches or passes goalDistance using no more than maxHops and never goes backwards from where he starts.

I used a while loop that continued as long as the frog still has hops remaining, has not passed the goal distance, and hasn’t gone backwards. Each time it loops the current position is incremented by hopDistance() and the hop is counted.

When it finished if the current position is greater than or equal to the goal distance the method returns true. Otherwise it returns false.

Part B

runSimulation gives you a set number of times to simulate a frog hop and has you return the percentage of times that the frog was able to successfully reach their goal distance.

For this I looped num times, calling simulate() inside each loop. Each time that simulate() returned true I incremented a counter. At the end it returns the number of successful simulations, cast as a double, divided by the number of tries to get the percentage of good simulations.

 

 

Why ClassCube?

Are you spending too much time grading your students' source code?

ClassCube is an online tool for Computer Science teachers that allows you to create lab assignments for your students. They'll submit their code online and ClassCube will automatically grade it for you.

Teaching with ClassCube…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>