Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this problem you will recreate the classic race of the turtle and the hare. Y

ID: 3800089 • Letter: I

Question

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 event. Our contenders begin the race at position 1. Their destination, 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 side of a slippery mountain, so occasionally the contenders lose ground and slip. They try to make up for those occasional slips fast plods (the turtle) and 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: 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 animal 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 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. You may also do text mode, 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). Hand in your program listing (source code with javadoc tags and generated html file and test runs to show that your program works.

Explanation / Answer

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

class Race{
int[] race = new int[75];
int turtle;
int hare;
int finishLine;
Random randomnumbers = new Random();

Race(){
hare = 1;
turtle = 1;
finishLine = 75;
}

public int moveTurtle(int pos, int finishLine){
//to randomize move
  
int min = 1;
int max = 11;

int percent = ThreadLocalRandom.current().nextInt(min, max + 1);
  

//now determine moves


if (percent >= 1 && percent <= 5)
turtle += 4;
//slip
else if (percent == 6 || percent == 7)
turtle -= 5;
//slow plod
else
turtle += 2;

// protect from going past start
if (turtle < 1)
turtle = 1;
// to make sure game ends
else if(turtle > 70)
turtle = 70;


return 0;

}// end turtle


public int moveHare(int pos, int finishLine){
// randomize move

int min = 1;
int max = 11;
int percent = ThreadLocalRandom.current().nextInt(min, max + 1);
// determine moves

if (percent == 3 || percent == 4)
hare += 10;
//big slip
else if (percent == 5)
hare -= 12;
// small hop
else if (percent >= 6 && percent <= 8)
++hare;
// )small slip
else if (percent > 8)
hare -= 2;
//ensure hare doesn't go past start
if (hare < 1)
hare = 1;
// ensure hare doesnt go past end
else if (hare > 70)
hare = 70;

return 0;
} // end movehare

public void printCurrentPositions(int turtlePos, int harePos){

int hare = harePos;
int turtle = turtlePos;

//this is the location of each on the array
for (int count = 1; count <= 75; count++){
// same spot
if (count ==turtle && count ==hare)
{
System.out.print("--");
}else if (count == hare)
{
System.out.print('H');
}
else if (count == turtle)
{
System.out.print('T');
}
else
{
System.out.print("-");
}
}
}
}
  

public class RaceStart{
public static void main(String[] args){
Race application = new Race();

//int turtle = 1;
//int hare = 1;
int finishLine = 75;

System.out.println("Race Started");

while (application.turtle < 75 && application.hare < 75){
application.moveHare(application.hare, application.finishLine);
application.moveTurtle(application.turtle, application.finishLine);
application.printCurrentPositions(application.turtle, application.hare);
} //end while

if (application.turtle > application.hare){
System.out.println(" TURTLE WINS!!");
}
else if(application.hare > application.turtle){   
System.out.println(" HARE WINS!!");
}
else if(application.hare == application.turtle){
System.out.println("TIE!!");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote