Turtle and Hare Race (Please code answer in java, and pay attention to ALL detai
ID: 3799074 • Letter: T
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!
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:
5 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).
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 slum esnora d her hscatacp of thi raa panaiasafdw .."V-seh imake upExplanation / Answer
Creatig below program to simulate your case:
Race.java
import java.io.IOException;
import java.util.Random;
/*
* Class to simulate the race
*/
public class Race {
// for generating randome no
Random r = new Random();
// move the turtle from pos on basis of random no
public int moveTurtle(int pos, int finishLine) {
int randomNo = r.nextInt(100);
if(randomNo < 28) {
// Slow plod
pos += 2;
} else if(randomNo < 65) {
// fast plod
pos += 4;
} else if(randomNo < 100) {
// slip
pos -= 5;
}
// if it crosses finish line, make pos=finishline
if(pos > finishLine) {
pos = finishLine;
}
// if its pos got less than 1
if(pos < 1) {
pos=1;
}
return pos;
}
public int moveHare(int pos, int finishLine) {
int randomNo = r.nextInt(100);
if(randomNo < 18) {
// Sleep
pos += 0;
} else if(randomNo < 33) {
// Big hop
pos += 10;
} else if(randomNo < 48) {
// Big slip
pos -= 12;
} else if(randomNo < 83) {
// Small hop
pos += 1;
} else if(randomNo < 48) {
// Small slip
pos -= 2;
}
// if it crosses finish line, make pos=finishline
if(pos > finishLine) {
pos = finishLine;
}
// if its pos got less than 1
if(pos < 1) {
pos=1;
}
return pos;
}
public void printCurrentPositions(int turtlePos, int harePos) {
System.out.println();
// print turtle pos
for(int i=1; i<= 75; i++) {
if(turtlePos==i) {
System.out.print("T");
} else {
System.out.print("_");
}
}
// print hare pos
System.out.println();
for(int i=1; i<= 75; i++) {
if(harePos==i) {
System.out.print("H");
} else {
System.out.print("_");
}
}
System.out.println();
}
public static void main(String arg[]) throws IOException {
Race r = new Race();
// variables to keep track of positions
int posHare = 1, posTurtle=1;
r.printCurrentPositions(posTurtle, posHare);
// keep moving till someone wins the race
while(posHare!=75 && posTurtle != 75) {
posHare = r.moveHare(posHare, 75);
posTurtle = r.moveTurtle(posTurtle, 75);
// print stats
r.printCurrentPositions(posTurtle, posHare);
}
// check who has won
System.out.println();
if(posHare == 75) {
System.out.println("******* Hare has won **********");
} else {
System.out.println("******* Turtle has won **********");
}
}
}
Sample Output:
T__________________________________________________________________________
H__________________________________________________________________________
____T______________________________________________________________________
H__________________________________________________________________________
T__________________________________________________________________________
_H_________________________________________________________________________
T__________________________________________________________________________
_H_________________________________________________________________________
__T________________________________________________________________________
_H_________________________________________________________________________
______T____________________________________________________________________
__H________________________________________________________________________
________T__________________________________________________________________
H__________________________________________________________________________
__________T________________________________________________________________
H__________________________________________________________________________
______________T____________________________________________________________
_H_________________________________________________________________________
_________T_________________________________________________________________
_H_________________________________________________________________________
_____________T_____________________________________________________________
_H_________________________________________________________________________
_______________T___________________________________________________________
_H_________________________________________________________________________
_________________T_________________________________________________________
___________H_______________________________________________________________
___________________T_______________________________________________________
___________H_______________________________________________________________
______________T____________________________________________________________
___________H_______________________________________________________________
_________T_________________________________________________________________
H__________________________________________________________________________
___________T_______________________________________________________________
_H_________________________________________________________________________
______T____________________________________________________________________
_H_________________________________________________________________________
________T__________________________________________________________________
_H_________________________________________________________________________
__________T________________________________________________________________
__H________________________________________________________________________
______________T____________________________________________________________
H__________________________________________________________________________
_________T_________________________________________________________________
_H_________________________________________________________________________
_____________T_____________________________________________________________
___________H_______________________________________________________________
_________________T_________________________________________________________
____________H______________________________________________________________
___________________T_______________________________________________________
_____________H_____________________________________________________________
______________T____________________________________________________________
______________H____________________________________________________________
__________________T________________________________________________________
_______________H___________________________________________________________
____________________T______________________________________________________
________________H__________________________________________________________
______________________T____________________________________________________
_________________H_________________________________________________________
__________________________T________________________________________________
__________________H________________________________________________________
______________________________T____________________________________________
______H____________________________________________________________________
_________________________T_________________________________________________
______H____________________________________________________________________
____________________T______________________________________________________
________________H__________________________________________________________
________________________T__________________________________________________
__________________________H________________________________________________
____________________________T______________________________________________
___________________________H_______________________________________________
_______________________T___________________________________________________
____________________________H______________________________________________
_________________________T_________________________________________________
______________________________________H____________________________________
_____________________________T_____________________________________________
__________________________H________________________________________________
________________________T__________________________________________________
___________________________H_______________________________________________
____________________________T______________________________________________
___________________________H_______________________________________________
_______________________T___________________________________________________
_____________________________________H_____________________________________
___________________________T_______________________________________________
______________________________________H____________________________________
_______________________________T___________________________________________
_______________________________________H___________________________________
___________________________________T_______________________________________
_______________________________________H___________________________________
_______________________________________T___________________________________
________________________________________H__________________________________
___________________________________________T_______________________________
_________________________________________H_________________________________
_______________________________________________T___________________________
_____________________________H_____________________________________________
___________________________________________________T_______________________
_________________H_________________________________________________________
_____________________________________________________T_____________________
__________________H________________________________________________________
_______________________________________________________T___________________
__________________H________________________________________________________
__________________________________________________T________________________
__________________H________________________________________________________
______________________________________________________T____________________
___________________H_______________________________________________________
_________________________________________________T_________________________
____________________H______________________________________________________
_____________________________________________________T_____________________
______________________________H____________________________________________
_________________________________________________________T_________________
_______________________________H___________________________________________
_____________________________________________________________T_____________
_______________________________H___________________________________________
_________________________________________________________________T_________
_________________________________________H_________________________________
_____________________________________________________________________T_____
___________________________________________________H_______________________
________________________________________________________________T__________
___________________________________________________H_______________________
___________________________________________________________T_______________
____________________________________________________H______________________
_____________________________________________________________T_____________
_____________________________________________________H_____________________
________________________________________________________T__________________
______________________________________________________H____________________
__________________________________________________________T________________
__________________________________________H________________________________
______________________________________________________________T____________
___________________________________________H_______________________________
__________________________________________________________________T________
___________________________________________H_______________________________
______________________________________________________________________T____
____________________________________________H______________________________
__________________________________________________________________________T
________________________________H__________________________________________
******* Turtle has won **********
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.