In this problem, you’ll recreate a version of the classic race of the tortoise and the hare. You’ll use a 𝑟𝑎𝑛𝑑𝑜𝑚 𝑛𝑢𝑚𝑏𝑒𝑟 𝑔𝑒𝑛𝑒𝑟𝑎𝑡𝑜𝑟 to simulate the race. The contenders begin race along a track of 100 squares. Each square represents a possible position along the race course. The finish line is at square 100. The first contender to reach or pass square 100 wins. 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 Figure 8. Use variables to keep track of the positions of the animals (i.e., position numbers are 1–
100). Start each animal at position 1 𝑡ℎ𝑒 “𝑠𝑡𝑎𝑟𝑡𝑖𝑛𝑔 𝑔𝑎𝑡𝑒” . If an animal slips before square 1, move it back to square 1.
Note:
- This is an individual assignment; please do your own work, sharing and/or copying code and/or solution ideas with/from others will result in a grade of 0 and disciplinary actions for all involved parties. If you run into problems and have done your best to solve them, please contact me before/after class or by e-mail. ü A 20% grade deduction for every day the assignment is late.
| Racer | Move Type | % of time | Direction | Squares to move and direction |
| Sleep | 10% | – | No movement | |
| Jump | 40% | Forward | Random # of Squares between 1 and 3 | |
| Slip | 30% | Backwards | Random # of Squares between 1 and 6 | |
| Walk | 20% | Forward | Random # of Squares between 0 and 1 | |
| Sleep | 10% | – | No movement | |
| Jump | 30% | Forward | Random # of Squares between 1 and 5 | |
| Small slip | 20% | Backwards | Random # of Squares between 1 and 2 | |
| Big Slip | 10% | Backwards | Random # of Squares between 1 and 7 | |
| Walk | 30% | Forward | Random # of Squares between 0 and 1 |
Figure 1: Rules for adjusting the positions of the Tortoise and the Hare.
Hint:
- Generate the percentages in Figure 1 by producing a random integer 𝑖 in the range 0 𝑖 9. For the Tortoise, perform a “𝐽𝑢𝑚𝑝” when 0 𝑖 3, a “𝑠𝑙𝑖𝑝” when 4 𝑖6, a “𝑤𝑎𝑙𝑘” when 7 𝑖8, or a sleep when 𝑖 9.
- Since you’re using random numbers, outputs will be different.
- To avoid long‐running simulations, stop the simulation if it goes over 200 iterations.
- A unit of time is one full iteration in the program. i.e. a counter shows how many iterations were made until one contestant wins. If the simulation takes 20 iterations, then this is treated as 20 seconds.
- Think of the 100‐square track as shown below. A forward move means, move up the track, a backward move means move down the track.
| start | … | finish |
0 1 2 3 4 5 6 7 8 9 10 … 99
𝑓𝑜𝑟𝑤𝑎𝑟𝑑 𝑚𝑜𝑣𝑒𝑠
What to submit:
- One Java class source file for class 𝑇𝑜𝑟𝑡𝑜𝑖𝑠𝑒𝐴𝑛𝑑𝐻𝑎𝑟𝑒 with the following minimum members (UML diagram Figure 8): 1. At least one use of:
- The WHILE loop
- The FOR loop
- The SWITCH decision structure
- 𝑀𝐴𝑋_𝑀𝑂𝑉𝐸𝑆, afinal, and static field with value 100
- 𝑚𝑎𝑖𝑛, starts the race by creating an instance of class 𝑇𝑜𝑟𝑡𝑜𝑖𝑠𝑒𝐴𝑛𝑑𝐻𝑎𝑟𝑒.
- Default constructor:
- Prints the start message in Figure 2
- Print the racer’s position using the method 𝑝𝑟𝑖𝑛𝑡𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛𝑠
- Move both players using the methods 𝑠𝑖𝑚𝑢𝑙𝑎𝑡𝑒𝑇𝑜𝑟𝑡𝑜𝑖𝑠𝑒𝑀𝑜𝑣𝑒 and 𝑠𝑖𝑚𝑢𝑙𝑎𝑡𝑒𝐻𝑎𝑟𝑒𝑀𝑜𝑣𝑒
- Repeat step 3 until either one wins, there is a tie, or the race times out (i.e. maximum of 200 simulations). Ø Print the results of the simulation. The result should state if:
- The Tortoise wins
- The Hare wins
- There is a tie
- There is a timeout. Also indicate who won.
- 𝑟𝑎𝑛𝑑𝑜𝑚𝐵𝑒𝑡𝑤𝑒𝑒𝑛, returns an integer random number between two limits (inclusive)
- 𝑝𝑟𝑖𝑛𝑡𝑃𝑜𝑠𝑖𝑡𝑖𝑜𝑛𝑠, prints the race track and shows the position of the Tortoise and the Hare. Use the Letter ‘T’ to represent the Tortoise, ‘H’ for the Hare, and ‘B’ if both are on the same square. See Figure 3
- 𝑠𝑖𝑚𝑢𝑙𝑎𝑡𝑒𝑇𝑜𝑟𝑡𝑜𝑖𝑠𝑒𝑀𝑜𝑣𝑒, a function which simulates the movements of the Tortoise as shown in Figure 1.
𝑠𝑖𝑚𝑢𝑙𝑎𝑡𝑒𝐻𝑎𝑟𝑒𝑀𝑜𝑣𝑒, a function which simulates the movements of the Hare as shown in Figure 1.



