(Simulation: The Tortoise and the Hare) In this problem, you will re-create the
ID: 3633756 • Letter: #
Question
(Simulation: The Tortoise and the Hare) In this problem, you will re-create the classic race of
the tortoise and the hare. You will use random-number generation to develop a simulation of this
memorable event.
Our contenders begin the race at square 1 of 70 squares. Each square represents a possible
position along the race course. The finish line is at square 70. The first contender to reach or pass
square 70 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.
A clock ticks once per second. With each tick of the clock, your application should adjust the
position of the animals according to the rules in Fig. 7.34. Use variables to keep track of the positions
of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (the “starting
gate”). If an animal slips left before square 1, move it back to square 1.
Generate the percentages in Fig. 7.34 by producing a random integer i in the range 1 <=i <=10.
For the tortoise, perform a “fast plod” when 1 <=i <=5, a “slip” when 6 <=i <=7 or a “slow plod”
when 8 <=i <=10. Use a similar technique to move the hare.
Animal Move type Percentage of the time Actual move
Tortoise Fast plod 50% 3 squares to the right
Slip 20% 6 squares to the left
Slow plod 30% 1 square to the right
Hare Sleep 20% No move at all
Big hop 20% 9 squares to the right
Big slip 10% 12 squares to the left
Small hop 30% 1 square to the right
Small slip 20% 2 squares to the left
Fig. 7.34 | Rules for adjusting the positions of the tortoise and the hare
Begin the race by displaying
BANG !!!!!
AND THEY'RE OFF !!!!!
Then, for each tick of the clock (i.e., each repetition of a loop), display a 70-position line showing
the letter T in the position of the tortoise and the letter H in the position of the hare. Occasionally,
the contenders will land on the same square. In this case, the tortoise bites the hare, and your application
should display OUCH!!! beginning at that position. All output positions other than the T, the
H or the OUCH!!! (in case of a tie) should be blank.
After each line is displayed, test for whether either animal has reached or passed square 70. If
so, display the winner and terminate the simulation. If the tortoise wins, display TORTOISE WINS!!!
YAY!!! If the hare wins, display Hare wins. Yuch. If both animals win on the same tick of the
clock, you may want to favor the tortoise (the “underdog”), or you may want to display It's a tie.
If neither animal wins, perform the loop again to simulate the next tick of the clock. When you are
ready to run your application, assemble a group of fans to watch the race. You’ll be amazed at how
involved your audience gets!
Later in the book, we introduce a number of Java capabilities, such as graphics, images, animation,
sound and multithreading. As you study those features, you might enjoy enhancing your
tortoise-and-hare contest simulation.
Explanation / Answer
please rate - thanks
import java.util.*;
public class tortouseAndHare
{public static void main(String []args)
{int finish=70,tort=1,hare=1,rtime=0;
System.out.println("ON YOUR MARK, GET SET BANG !!!!! AND THEY'RE OFF !!!!! ");
do
{hare=movehare(hare);
tort=movetort(tort);
print(tort,hare);
rtime++;
}while(tort<finish&&hare<finish);
if(tort>hare )
System.out.println("TORTOISE WINS!!! YAY!!! ");
else if(tort<hare )
System.out.println("Hare wins. Yuch. ");
else
System.out.println("Would you believe IT'S A TIE!! ");
System.out.println("time of race: "+rtime+" simulated seconds ");
}
public static void print(int t,int h)
{int i;
if(h==t)
{for(i=0;i<h;i++)
System.out.print(" ");
System.out.println("OUCH!!!");
}
else if(h<t)
{for(i=0;i<h;i++)
System.out.print(" ");
System.out.print("H");
for(i=0;i<(t-h);i++)
System.out.print(" ");
System.out.print("T");
}
else
{for(i=0;i<t;i++)
System.out.print(" ");
System.out.print("T");
for(i=0;i<(h-t);i++)
System.out.print(" ");
System.out.print("H");
}
System.out.println();
}
public static int movehare(int r )
{int num;
num=(int)(Math.random()*10);
if(num<2)
r-=2;
else if(num<5)
r++;
else if(num<6)
r-=12;
else if(num<8)
r+=9;
if(r< 1 )
r=1;
return r;
}
public static int movetort(int t)
{int num;
num=(int)(Math.random()*10);
if(num<5)
t+=3;
else if(num<7)
t-= 6;
else
t++;
if(t<1)
t=1;
return t;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.