• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Need help with Thread Program

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I am very new to threads and would like some help on getting this started. Thanks!
Be able to:

1. Write a class that extends class Thread
2. Create and Use objects of that class
3. Modify and manipulate variables based on the object’s name
4. Start threads
5. Put threads to “sleep”
6. End a loop based on object’s static variable


/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/
Your Programming Tasks
1. Create a RaceCarThread class that extends Thread. RaceCarThread will
a. Have 2 instance variables
i. A static Boolean: raceNow  (shared by all instances)
ii. An double: lapsCompleted  (unique to each instance
b. A Constructor that
i. Accepts the object’s name as a String
ii. Calls the super constructor with the object’s name
iii. Sets raceNow to true
iv. Sets lapsCompleted to zero
c. A run method that
i. Will loop until ONE of the cars has reached 15 lapsCompleted
1. Generate a random number between 0 and 1. Add the random number to the laps completed
2. If the car has completed 15 or more laps do 3 things…you determine the order
a. break out of the loop      
b. Turn off raceNow        
c. Declare the current object the winner by printing an appropriate message that includes the car’s name

3. Will check the object’s name to
a. Display the car’s name, number of laps completed, number of laps remaing
b. Put the thread to sleep.  The longer the sleep the slower the car…more or less. The sleep times should be random and should not exceed 333 milliseconds.  Use the techniques shown in example 4 from class to create random sleep times.  I suggest that you use the Random class nextInt method rather than the Math.random method.
c. Each lap each car should have a new sleep time.
d. Calculate and display the total sleep time for each car at the end of the race. This is not shown in the sample output.
ii. This completes the loop in the run method

d. This completes the RaceCarThread

2. Write a tester program “RaceCarThreadTest”.  This class will
a. Create and instantiate five objects of RaceCarThread. The objects are
i. corvette,
ii. ferrari,
iii. viper,
iv. audi,
v. porsche,
b. Send the names of the objects to the constructor. The names of the objects will be
i. Corvette,
ii. Ferrari,
iii. Viper,
iv. Audi,
v. Porsche,
c. Start each of the RaceCarThreads.
d. This completes the RaceCarThreadTest



   

Sample output:  
Your results will differ.  
You should get different results with each race.
You should not have more than 1 winner declared.
You should run multiple races to insure proper results
Please note that the line separators “**********”
are only added to aid in readability.  The line
is printed after the Porsche information, however,
due to the nature of threads,  it will not always follow
DIRECTLY after the Porsche line.










/- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/


Deliverables:

Turn in a zipped folder that contains:

1. RaceCarThread
2. RaceCarThreadTest
 
Sheriff
Posts: 17288
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you done so far? I hope you’re not expecting anyone to do the legwork for you. That’s not what this site is for.
 
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ask why you have been told to extend Thread; that is not the usual way to start a thre‍ad.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic