In Java: I got most of the logic down but I\'d like to seen an answer that actua
ID: 3857972 • Letter: I
Question
In Java: I got most of the logic down but I'd like to seen an answer that actually prints out the T and H and moves them or those something with an image since I only print out the winner and responses with my own code.
This project involves writing a program to simulate a tortoise and hare race. The contenders will each race along a horizontal course that contains at least 50 squares. You may add more if you wish. The race begins with each contender at square 1. The contender that first reaches or passes the last square of the course is the winner of the race. The following table indicates the types of moves that each contender can make Contender Type of Move Time % age Result of Move Tortoise Fast Plo 50 % 3 squares to right Slow 30 % 1 squares to right Slip 20 % 6 squares to left HareBig Hop 20 % 9 squares to right Small h Big Slip Small Sli Fall a sleep 10% 20% …… 20 % 1 squares to right 12 squares to left 2 squares to left Each contender starts at position 1. When a contender slips, they can't slip any further left than position 1. You will use a random number generator to simulate the percentages of each type of move indicated in the table. Generate a random integer, n, in the range 1 sns 10. For the tortoise, perform a fast plod if the number is 1-5, a slow plod if the number is 6-8, and a slip if the number is 9-10. For the hare, perform a big hop if the number is 1-2, a small hop if the number is 3-5, a big slip if the number is 6, a small slip if the number is 7-8, and fall asleep if the number is 9-10. There are a number of ways to design this program. One way would be to have a looping construct be the overall controller of things. Each iteration would adjust the contender positions, and the loop would terminate when one of the contenders reaches the last square of the race course. You will decide on an approach as part of your design step You must keep track of each contender's position and display it each time positions change. Show the letter T in the position of the tortoise, and the letter "H' in the position of the Hare. It is possible for the contenders to land on the same square. When this happens, the tortoise bites the hare, and your program should display "OUCHIl" beginning at that square. All output positions other than the T, the "H, and the OUCH should be blank. If the tortoise wins, display "TORTOISE WINSII". If the hare wins, display HARE WINSII". If the race is a tie, display "IT'S A TIEll". At the beginning of the race, display "AND THEY'RE OFFIl You must use an object oriented approach to designing and implementing this program. Be sure to comment your code and use good programming styleExplanation / Answer
Given below is the code for the question along with output. Please rate the answer if it helped. Thank you.
Hare.java
public class Hare {
private int position;
private int max_position;
public Hare(int max)
{
position = 1;
max_position = max;
}
public int getPosition()
{
return position;
}
private void move(int x)
{
position += x;
//make sure position is in range 1-max_position
if(position < 1)
position = 1;
else if(position > max_position)
position = max_position;
}
public void bigHop()
{
move(9);
}
public void smallHop()
{
move(1);
}
public void bigSlip()
{
move(-12);
}
public void smallSlip()
{
move(-2);
}
public boolean finishedRace()
{
return position == max_position;
}
}
Tortoise.java
public class Tortoise {
private int position;
private int max_position;
public Tortoise(int max)
{
position = 1;
max_position = max;
}
public int getPosition()
{
return position;
}
private void move(int x)
{
position += x;
//make sure position is in range 1-max_position
if(position < 1)
position = 1;
else if(position > max_position)
position = max_position;
}
public void fastPlod()
{
move(3);
}
public void slowPlod()
{
move(1);
}
public void slip()
{
move(-6);
}
public boolean finishedRace()
{
return position == max_position;
}
}
Race.java
import java.util.Random;
public class Race {
private static void printTrack(int tick, int length, Hare hare, Tortoise tortoise)
{
System.out.printf(" Tick %3d: ", tick);
for( int i= 1; i <= length; i++)
{
if(i == hare.getPosition() && hare.getPosition() == tortoise.getPosition())
System.out.printf("[OUCH!!] ");
else if(i == hare.getPosition())
System.out.printf("[H]");
else if(i == tortoise.getPosition())
System.out.printf("[T]");
else
System.out.printf("[]");
}
System.out.printf(" ");
}
public static void main(String[] args) {
int track_length = 50;
Hare hare = new Hare(track_length);
Tortoise tortoise = new Tortoise(track_length);
int tick = 1;
Random random = new Random(System.currentTimeMillis());
System.out.println("AND THEY'RE OFF!!");
printTrack(tick, track_length, hare, tortoise);
while(!hare.finishedRace() && !tortoise.finishedRace())
{
tick++;
int n = 1 + random.nextInt(10);
//generate move for hare
if(1 <= n && n <=2) //20 % big hop
hare.bigHop();
else if(3<=n && n <=5) //30% small hop
hare.smallHop();
else if(n == 6) //10% , big slip,
hare.bigSlip();
else if(7<=n && n<=8) //20% small slip
hare.smallSlip();
//no move if 9 and 10 , harre is asleep
//generate move for tortoise
if(1 <= n && n <=5) //50 % fast plod
tortoise.fastPlod();
else if(6<=n && n <=8) //30% slow plod
tortoise.slowPlod();
else //20% , slip
tortoise.slip();
printTrack(tick, track_length, hare, tortoise);
}
if(tortoise.finishedRace())
if(hare.finishedRace())
System.out.println("IT'S A TIE");
else
System.out.println("TORTOISE WINS!!");
else
System.out.println("HARE WINS!!");
}
}
output
AND THEY'RE OFF!!
Tick 1: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 2: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 3: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 4: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 5: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 6: [][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 7: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 8: [H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 9: [][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 10: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 11: [][][][][][][][][][OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 12: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 13: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 14: [][][][T][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 15: [T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 16: [][][][T][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 17: [H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 18: [][][][][][][][T][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 19: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 20: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 21: [][H][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 22: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 23: [][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 24: [H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 25: [H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 26: [H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 27: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 28: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 29: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 30: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 31: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 32: [H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 33: [][H][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 34: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 35: [][][][][][][][][][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 36: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 37: [][][][][][][][][][][][][][][][][][][T][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 38: [][][][][][][][][][][][][][][][][][H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 39: [][][][][][][][][][][][][][][][][][][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 40: [][][][][][][H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 41: [][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]
Tick 42: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 43: [][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 44: [][][][][][][][][][][][][][][H][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 45: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 46: [][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]
Tick 47: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 48: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 49: [][][][][][][][][][][][][][][][][][][][][H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]
Tick 50: [][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 51: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 52: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 53: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]
Tick 54: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][]
Tick 55: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 56: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 57: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][]
Tick 58: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]
Tick 59: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 60: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 61: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]
Tick 62: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 63: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 64: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 65: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 66: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 67: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 68: [H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]
Tick 69: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]
Tick 70: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 71: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]
Tick 72: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 73: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 74: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 75: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 76: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]
Tick 77: [][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 78: [][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 79: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T][][][][][][][][][][][][]
Tick 80: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][T][][][][][][][][][]
Tick 81: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][T][][][][][][]
Tick 82: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][T][][][]
Tick 83: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T]
TORTOISE WINS!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.