Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java This is a Java programming assignment where all of the specifications must be met. Help would be appreciated! Project 23-2: Tortoise and Hare Race

Java

This is a Java programming assignment where all of the specifications must be met. Help would be appreciated!

Project 23-2: Tortoise and Hare Race

Create an application that uses threads to simulate a race between two runners that differ in their speed and how often they need to rest.

Console:

image text in transcribed

Specifications:

Each runner should be implemented as a separate thread using a class named RunnerThread. This class should include these instance variables:

  • a string representing the name of the runner
  • an int value from 1 to 100 indicating the likelihood that on any given move the runner will rest instead of run
  • an int value that indicates the runner's speed (that is, how many meters the runner travels in each move)
  • an int value indicating the runner's progress on the course

The run() method of the RunnerThread class should consist of a loop that repeats until the runner has reached 1000 meters. Each time through the loop, the thread should decide whether it should run or rest based on a random number and the percentage passed to the constructor. If this random number indicates that the runner should run, the method should add the speed value to the position value and display the new position. The run() method should sleep for 100 milliseconds on each repetition of the loop. When the loop ends, this method should display a message indicating that the runner has finished.

The main() method of the application's main class should create two runner threads and start them. One of the threads should be named 'Tortoise.' It runs only 10 meters each move, but plods along without ever resting. The other thread should be named 'Hare.' It should run 100 meters each move, but should rest 90% of the time.

To determine whether a thread should run or rest, calculate a random number between 1 and 100. Then, have the thread rest if the number is less than or equal to the percentage of time that the thread rests. Otherwise, the thread should run.

Modify the main class so it runs the race 100 times and reports how many times each runner wins. (To make the application run faster, you may want to reduce the sleep time in the runner threads.)

Modify the application so it can support up to 9 runners.

Add an additional random element to the runner's performance. For example, have a 'clumsiness percentage' that indicates how often the runner trips and hurts himself. When the runner trips, he sprains his or her ankle and can run only at half speed for the next five moves.

Add the ability for runners to interfere with each other. For example, have an 'orneriness percentage' that indicates how likely the runner is to trip another runner who is passing him. This will require additional communication among the threads.

Hare: I finished! Tortoise: I finished

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions