Question
Turtle and Hare Race (Please code answer in java, and pay attention to ALL details and comment code so its easy to understand). Thank you!
Turtle and Hare Race (Please code answer in java, and pay attention to ALL details and comment code so its easy to understand). Thank you!
In this problem you will recreate the classic race of the turtle and the hare. You will use random number generation and method development to simulate this even. Our contenders begin the race at position 1. Their destinateion (i.e. the finish line) is at position 75. The first contender to reach or pass position 75 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the slide of a slippery mountain, so occasionally the contenders lose ground and slip. They try to make up for those occasional slips by fast plods (the turtle) and big hops (the hare. Our hare is so sure of his advantage over the turtle that he takes short naps every now and then. In each step of the race the position of the animals should be adjusted according to the following rules:
Animal | Move type | % of time | Actual move |
Turtle | |||
Fast plod | 37% | 4 squares forward | |
Slip | 35% | 5 squares backward | |
Slow plod | 28% | 2 squares forward | |
Hare | |||
Sleep | 18% | No move | |
Big hop | 15% | 10 squares forward | |
Big slip | 15% | 12 squares backward | |
Small hop | 35% | 1 square forward | |
Small slip | 17% | 2 squares backward |
Use variables to keep track of the positions of the animals (i.e. position numbers are 1-75). Start each animal at position 1 (i.e. the "starting gate"). If an animal slips back before square 1, move the naimal back to square 1. If the animal advances past the finish line, set the position to square 75. For each step of the race (i.e. each repetition of a loop), print a line showing the letter 'T' in the position of the turtle and a line showing the letter 'H' in the position of the hare. All positions other than the T or the H should be blank. After each line is printed, test if either animal has reached or passed square 75. If so, print the winner and terminate the simulation. Otherwise, the race continues.
Your program should implement and use the following methods:
public int moveTurtle(int pos, int finishLine)
public int moveHare(int pos, int finishLine)
public void printCurrentPositions(int turtlePos, int harePos)
The first two functions take in the current position of the turtle/hare and, based on a randomly generated number, compute the new position following the rules in the table above. The third function takes positions of both animals and produces the "image" on the screen reflecting the current state of the race (using T for turtle and H for hare). The main method needs to repeatedly call these three methods until one of the animals reaches the finish line (at position 75).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started