• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Java Threads exercice

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I have to do an important exercice in java using thread and i dont understand at all

I put the enunciated after so it's more clear better to look what to do ( i passed long time translating it into english, tried my best;*-*)

my code.. :
Part1:

part2:


The enunciated /what i have to do: :/
-Thread-
General remarks:
- Classes can have attributes but they must be protected.
- Think about managing exceptions


Warning: Please read the entire subject carefully before starting.
A developer is planning to build a hotel with 250 rooms. To do this, he wants to create a simulation
Using the principle of producer / consumer and cooperative synchronization


I) Hotel class
Write the Hotel class that includes
1) A constructor
2) A checkIn () method representing the arrival of a person in the hotel
3) A checkOut () method representing the departure of a person from the hotel

Remarks :
It is impossible to simulate a person reserving a room if the hotel is full and it is
Impossible to simulate the exit of a person of the hotel if there is nobody in the hotel.


II)Person class
Write the class Person which has a constructor to assign at least:
1. A name
2. The number of stays(or sojourn) that the person passed at the hotel (numberOfStays)
3. The duration of each stay passed at the hotel (stayDuration)


III) ArrivingPerson and OutgoingPerson
- Write the ArrivingPerson class, to manage different people arriving at the hotel in
parallel. They come numberOfStays times and sleep at the hotel during stayDuration. At the
end of stays, display the name of the person and indicate that he has made his last arrival.

- Write the OutgoingPerson class to manage different people leaving the hotel in
parallel. 
This class also contains, in attribute, the duration of the departure of the hotel timeToPart and the action is repeated numberOfStays times.
At the end of the stays, display the person's name then indicate that the person made his last departure.
(Remarks: Think about the mechanisms of inheritance)


IV) ManagementHotel
Write the ManagementHotel class containing the main method to manage a table of 1000 people consisting of 500 people arriving at the hotel and 500 people leaving the hotel.
The Parallel simulation of persons must also be in this class. Instantiate the parameters of
simulation with random numbers.
For it :
- Put all the simulation in the method public static void main (String [] args)
- Define a variable populationMax and instantiate it at the right value
- Creating Population, a Person Table
- Instantiate  numberOfStays  ∈  [0,10]
- Instantiate half of population as ArrivingPerson ,with for each person  stayDuration ∈ [0,100]
- Instantiate the second half of the population as an OutgoingPerson, with for each person timeToPart ∈ [0,75]
- Prepare and run the simulation in parallel with populationMax threads
- Indicate when the simulation is finished

Remarks:
- Think of reserving the memory boxes of the tables
- Think of using thread operations only to objects that allowing it (this is not the case for Person, for example).



Thanks all
 
Rancher
Posts: 976
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pierre,

If you have a question in there someplace I missed it.  There are dozens of people that will be happy to help you with specific questions, but not do your homework for you.

If you truly do not have a clue, then I suggest you talk with your instructor and see if he can give you places to look in your text to help you out, but here are good resources available on the net:

Oracle--Java Tutorial
Oracle--The Really Big Index

Les
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think your line 14 will compile. A throws is (I think) only permitted after the name of a method, so you would have to write the return type, method name and (...) after synchronized and before throws.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks , but i really don't understand, but i try

so in part I:
I don't know what others attributes than capacity (nomber of room)there is in the class
I don't know what to write in the function checkOut and CheckIn i write something but i dont really know:
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the time has come to turn yout computer off and write down what you are trying to do on paper. Only when you have that worked out will you be able to write any code.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think the time has come to turn yout computer off and write down what you are trying to do on paper. Only when you have that worked out will you be able to write any code.



Im not better on a paper, i dont see my mistakes, but what do you think of my function checkOut and checkIn?
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say aloud what you think those methods shou‍ld do.
 
Sheriff
Posts: 26776
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the "capacity" of a hotel -- it's the maximum number of people who can stay in the hotel at one time. That's what "capacity" means. But "size"? I don't understand what the "size" of a hotel is. Perhaps you could explain? Yes, I realize that having to work in English makes that sort of thing harder... perhaps you thought of a specific word in French (which it looks to me is your native language) but your English translation as "size" is not at all specific.

And don't be afraid of long variable names: if you want a variable to store the number of available rooms then numberOfAvailableRooms is a good choice for that variable's name.
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I understand the "capacity" of a hotel -- it's the maximum number of people who can stay in the hotel at one time. That's what "capacity" means. But "size"? I don't understand what the "size" of a hotel is. Perhaps you could explain? Yes, I realize that having to work in English makes that sort of thing harder... perhaps you thought of a specific word in French (which it looks to me is your native language) but your English translation as "size" is not at all specific.

And don't be afraid of long variable names: if you want a variable to store the number of available rooms then numberOfAvailableRooms is a good choice for that variable's name.



Yes you're right ;)
For me capacity is the maximum number of rooms of the hotel(250) and size( yes i chosed a bad name) is the number of rooms occupated (by a person) ;))
Yes a long variable name is better ;)
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so i changed a little.. :
 
Les Morgan
Rancher
Posts: 976
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pierre,

Since you are using synchronized in your methods, I suggest you change your declaration of numberOfOccupatedRooms to a protected static int.  This will make it a class variable and tie any and all threads to one instance of numberOfOccupatedRooms.  As it stands now in your code, the use of a protected int is an instance variable. Instance variables will tie to any particular instance, so if you have 2 instances running, then your number of occupied rooms will be tracked separately in each instance and allow maximum checkins in each instance, 250 in the first instance and 250 more in each additional instance.  Without the use of a static qualifier, Class Variable, you will be able to double, triple, or more book your rooms according to how many instances you have running.

Les
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I worry whenever I see anything marked static. Making the variable static would suggest every hotel has the same number of rooms. Are you sure about that? Shou‍ld the exercise use one instance reached by several threads? Is this possible in view of the tight specifications above?
 
Pierre Gadar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Since you are using synchronized in your methods, I suggest you change your declaration of numberOfOccupatedRooms to a protected static int.  This will make it a class variable and tie any and all threads to one instance of numberOfOccupatedRooms.  As it stands now in your code, the use of a protected int is an instance variable. Instance variables will tie to any particular instance, so if you have 2 instances running, then your number of occupied rooms will be tracked separately in each instance and allow maximum checkins in each instance, 250 in the first instance and 250 more in each additional instance.  Without the use of a static qualifier, Class Variable, you will be able to double, triple, or more book your rooms according to how many instances you have running.


ok, thanks


Campbell Ritchie wrote:I worry whenever I see anything marked static. Making the variable static would suggest every hotel has the same number of rooms. Are you sure about that? Shou‍ld the exercise use one instance reached by several threads? Is this possible in view of the tight specifications above?



But I think in the exercice, there is only one hotel
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic