Starting from:

$30

CSC221 Assignment 2 – Solved

Assignment 2 –

How to submit: upload JAVA files to Blackboard

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 
 

 

More products