Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I'm having troubles with this Java assignment, and I can't find a good example to go off of. Here is the assignment. Create an

Hi, I'm having troubles with this Java assignment, and I can't find a good example to go off of. Here is the assignment.

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

Get set...Go!

Tortoise: 10

Hare: 100

Tortoise: 20

Tortoise: 30

Tortoise: 40

Tortoise: 50

Tortoise: 60

Tortoise: 70

Tortoise: 80

Tortoise: 90

Tortoise: 100

Tortoise: 110

Tortoise: 120

Tortoise: 130

Tortoise: 140

Tortoise: 150

Hare: 200

Tortoise: 160

Tortoise: 170

Tortoise: 180

Tortoise: 190

Tortoise: 200

Hare: 300

Tortoise: 210

Tortoise: 220

Hare: I finished!

Tortoise: 230

Tortoise: 240

Tortoise: 250

Tortoise: 260

Tortoise: 270

Tortoise: 280

Tortoise: 290

Tortoise: 300

Tortoise: I finished!

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.

Hint

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.

Enhancements

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago