$40
How to submit: upload JAVA files to Blackboard Assignment 2 –
The Tortoise and the Hare, a race simulation.
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
2. ππ΄π_ππππΈπ, afinal, and static field with value 100
3. ππππ, starts the race by creating an instance of class ππππ‘πππ ππ΄πππ»πππ.
4. 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:
1. The Tortoise wins
2. The Hare wins
3. There is a tie
4. There is a timeout. Also indicate who won.
5. πππππππ΅ππ‘π€πππ, returns an integer random number between two limits (inclusive)
6. πππππ‘πππ ππ‘ππππ , 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
7. π πππ’πππ‘πππππ‘πππ ππππ£π, a function which simulates the movements of the Tortoise as shown in Figure 1.
8. π πππ’πππ‘ππ»ππππππ£π, a function which simulates the movements of the Hare as shown in Figure 1. Grading:
Item
Points
Comments (Javadoc and major steps)
10
Static members
10
WHILE loop
5
FOR loop
5
Switch
5
Race simulation until one wins, tie, or timeout
10
Boundary checks
5
Constructor
5
randomBetween
10
printPositions
10
simulateTortoiseMove
10
simulateHareMove
10
Correct output
5
100
Figures:
ππ ππππ
ππ΄π
πΎ, πΊπΈπ ππΈπ π΅π΄ππΊ !!!!!
π΄ππ· ππ»πΈπ′π
πΈ ππΉπΉ !!!!!
Figure 2: initial message printed when the race starts
Figure 3: With every iteration, the racer’s position is printed. Positions differ with every iteration and each simulation.
πππ
πππΌππΈ ππΌππ!!! ππ΄π!!!
ππππ πΈππππ ππ 766 π ππππππ
Figure 4: Message if the Tortoise wins. Time differs for each simulation.
π»πππ π€πππ . ππ’πβ!
ππππ πΈππππ ππ
πΌπ‘′π π π‘ππ
305 π ππππππ
Figure 5: Message if the Hare wins. Time differs for each simulation.
ππππ πΈππππ ππ
527 π ππππππ
Figure 6: Message if a tie occurs. Time differs for each simulation.
ππππ ππ’π‘!
π»πππ π€πππ . ππ’πβ!
ππππ πΈππππ ππ 2000 π ππππππ
Figure 7: Message if a timeout occurs after 2000 iterations (seconds). The winner message is also printed.
Figure 8: Class Diagram – UML legend shown below
UML Diagram Legend
Symbol
Description
Underlined
Indicates a static member
A private member (i.e. variable or method)
A private final member (i.e. variable)
A public field (i.e. variable or method)
A public abstract member (i.e. variable or method)
A public constructor
A static public member
An interface
A public class
A public abstract class
A hollowed arrow indicates inheritance
An open-ended arrow indicates composition
A dotted line and hollowed arrow indicate class implementation