Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 79

Thread: Queue Simulation

  1. #51
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Oh oki so everytime we now println() of watever object get call the toString() method changes the [Station@8955b34] to the acutal customer name. Ive change the file name as you said to something else called it Station. You also read that he time the object was added to the queue, how is this ? I taught we have to the timer for this, sorry if my questions sound silly.

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    If the model is going to use clock time, the time the customer is added (and removed) could come from the System class's currentTimeMillis() method.

    Next try defining the cashiers array.
    Add code to the generator loop to check if there is an empty slot in the cashier array and if so, to add the customer to the array and set its time in queue value to 0 (it didn't have to wait).
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    OKi ive done the generator loop for the empty slot in the cashier array based on what the user input how many cashiers will exist. But the whole thing is being yellow underlined and its asking me to Add @Suppress Warning 'un-used' to 'run()'

    int cashier_input = input.nextInt();
    Station[] cashiers = new Station[cashier_input];
    		if (cashiers == null)
    		{
    			try 
    			{
    				line.remove();
    				Thread.sleep(3000);
    			} 
    			catch (InterruptedException e) 
    			{
    				System.out.println("Sleep ERROR");
    			}
    			finally
    			{
    				System.out.println(line);
    			}
    		}

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    The cashiers process Customers not Stations.

    Do you know how to use an array? Take a look at the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    What is the posted code supposed to do? I don't understand why there is a call to the remove method.

    What is the sleep(3000) for? There should NOT be any hardcoded numbers in the code. All program controlling values should be assigned to a variable at the start of the class. Add an item to this list:
    	int cashier = 3;  //  hard code for testing -- add reading from user later
    	private int max_customers = 10 - 20;
    	private int max_arrival = 2;     // 2 sec
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    the code that I post basically is to remove the current customer from the queue and wait for 3000ms - 3sec untill it removes the next customer. Sorry norm I understand I had to change the current loop not make another one.

    to add the customer to the array and set its time in queue value to 0 (it didn't have to wait).
    I already known that to remove a customer from the queue you simply use line.remove() method. But what is the method to move the customer into the array list ?
    And what do you mean set the time in queue to 0 ?

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    wait for 3000ms - 3sec untill it removes the next customer.
    ??? The scaling of time means 3ms is used for 3sec
    What can happen in 3 seconds? That is not a reasonable time to do anything.

    time in queue value to 0 (it didn't have to wait).
    There is no wait time if the customer goes directly to a cashier when it arrives.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    noo i put the 3 seconds wait there so it simulates that the cashier take 3 seconds to process a customer.

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    cashier take 3 seconds to process
    Impossible and unrealistic.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Norm the part where I have remove the customer from the queue and into the cashier station I cant find any syntax which allows you to do that ?

    The queue basicall adds, remove and thats is is a first in first out sort thing.

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    remove the customer from the queue
    There is a method that removes an element from the list.
    into the cashier station
    Use an assignment statement to assign a value to a slot in an array.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Oki I didnt put everything in the number generator loop but on a separate loop:

    Station[] cashiers = new Station[cashier];
    		for (int i = 0; i < 10; i++)
    		if (cashiers != null)
    		{
    			cashiers[1] = line.poll();
    			System.out.println(line);
    		}

    It seems to work removing each customer from the queue untill the cashiers array is null.

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    Why is the cashiers an array of Station?

    Why is there a 10 in the for loop? That should be a variable defined at the top of the class.

    untill the cashiers array is null.
    The array shouldn't be null. The slots in the array could have null values.

    What is the purpose of the code in post#61?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    The customer removed from the queue goes into the slot right thats what the line.poll does. An ive replaced the 10 with the max_customers
    private int max_customers = 10; //FOR TESTING
    the cashier ! = null isnt that the same thing saying that all the slots values are null ?

    for (int i = 0; i < max_customers; i++)
    		if (cashiers != null)
    		{
    			cashiers[0] = line.poll();
    			System.out.println(line);
    		}

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    I think its time to go back to the design and describe what you are trying to simulate.
    My design was for a company like a bank that had a number of tellers(I). Customers(II) arrive at a variable rate(III) during the day. If there is cashier free, they go directly to the cashier, otherwise they wait in line(A) until a cashier is free. A cashier takes a variable amount of time(IV) to process the customer. When the customer is done, the cashier motions for the customer at the head of the line to come to the cashier(B).

    The time the customer waits in queue is the from the time of (A) to the time of (B).
    The variables are: I, II, III and IV

    cashier ! = null isnt that the same thing saying that all the slots values are null ?
    Not at all. There is no single statement to test the contents of all the slots of an array.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Yes I understand the design theory but its a whole, different story when you try to implement it, using the right methods, syntax etc. And the reason why I made cashiers an array of Station is because it wouldn't allow me to use the .poll() method to remove the customer.

    To me this look oki If I carry on and mess about with it I can hopefully get what this assignment wants me to produce.

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    what this assignment wants me to produce.
    Do you know what that is?

    wouldn't allow me to use the .poll() method to remove the customer.
    That doesn't make any sense. How was it coded? Working with Station objects when Customer objects have the data???
    Customers wait in the queue. Customers are processed by the cashiers.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    the reason why the whole thing went into station was because you suggested to rename the class and use a sub class called customer to hold all the data. And to be honest it doesnt really matter what names are as long as I can print each stage of the queue be processed at show the time spent in the queue.

    Aftet array list how do I come about recording the arrival time exit time to output each customers time spent waiting ?

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    how do I come about recording the arrival time exit time
    When the customer is added to the wait queue, set the customer's arrival time to the current time.
    When the customer is removed from the wait queue, compute the waiting time by subtracting the arrival time from the current time.

    t doesnt really matter what names are
    BTW names are important. They make it easier to understand what the classes and variables are used for. Its legal to name things: a, b, c, d, e, etc but that code would be very hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #69
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    and then how do I output the customer name as well as the wait time or is it just going to keep saying customer@fjf272638b etc

  20. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    What happened to the Customer class's toString() method? See post#50.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #71
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    in the loop when the customer is added to the queue how would you implement the .getTime method inside the loop.

  22. #72
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    What class is the getTime() method in and what does it return?
    What is the problem calling a method from inside a loop?

    The System class has a method that returns the PCs time.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #73
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    when I get the current time I then store it into the arrival time which is in the customer class where the data is store right and from the i nee to get the exit again store that in the customer class work the difference with is wait time and from the I just print out wait along with the customer name by using the toString method

  24. #74
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Norm ive done this to the current loop:
    try 
    				{
    					Thread.sleep(wait * 1);
    					line.add(new Station());
    					System.out.println(line);
    					System.currentTimeMillis();
     
    				}

    So now its getting the current system times every time its adding a new customer into the queue however how do I set the arrival_time in the customer class, I dont known the correct coding ?

  25. #75
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Queue Simulation

    how do I set the arrival_time in the customer class,
    If the variable is public use an assignment statement to assign it the value returned by: System.currentTimeMillis();.
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Multi-queue simulation theory
    By Herah in forum Object Oriented Programming
    Replies: 4
    Last Post: April 4th, 2013, 10:38 PM
  2. Cash Register Simulation Help
    By Gont in forum Object Oriented Programming
    Replies: 1
    Last Post: September 22nd, 2012, 06:58 PM
  3. create a simulation
    By aecosis in forum Java IDEs
    Replies: 3
    Last Post: June 12th, 2012, 08:55 AM
  4. Blackjack simulation program help
    By senorfletch in forum Java Theory & Questions
    Replies: 2
    Last Post: April 5th, 2011, 09:22 AM
  5. [SOLVED] Dice Rolling Simulation
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2011, 06:51 PM