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 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 79

Thread: Queue Simulation

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    This thread has a good description: Multi-queue simulation theory

    I don't remember seeing any assignments like this one you are working on.

    What techniques have you been shown in class that could be useful in doing this assignment?
    The clock time model uses threads, timers and synchronized queues.
    The event time model uses none of the above.
    They both use a random number generator.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    the link you provided that person is simulating multiple queues for that he needs to use timers threads. It seems like if there is only 1 queue to simulate you need to you event time model. In terms of what we been shown in class is simple loops, if statements and how use methods.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    In the clock time model, the timers would be for the cashier processing.
    shown in class is simple loops, if statements and how use methods
    I can't see that you have the knowledge yet to write a clock time model.
    The event time model requires random numbers which you say your comfortable with. There is one thing you'll also need:
    Have an event list (sorted according according to time, smallest time value is at the top of the list).
    This would require a PriorityQueue and classes that implement the Comparable interface.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    Norm I've done alot of the coding I didnt use real time but for now just attached the name and time in a separate class as methods. What the problem is im trying to catch the exception when the user enters a letter instead of a integer. However the Scanner object and is within the curly brackets for that "for" statement. Because of this im not able to use the Scanner user input of the cashier in my loop, its in the part where im dividing the
    Thread.sleep((wait * 1000)[]
    with the cashier input of the user hence simulating that there are more cashiers and the queue get processed faster.. How can I over come this problem ?

    public class Station 
    {
    	public static Queue<Customer> line = new  LinkedList<Customer> ();
    	static Scanner input = new Scanner (System.in);
    	public static void main(String[] args) 
    	{	
    		String[] list = {"Customer 1", "Customer 2", "Customer 3", "Customer 4", "Customer 5", "Customer 6", "Customer 7", "Customer 8", "Customer 9", "Customer 10", };
    		int customerlist = list.length;
     
    		for (int x = 0; x < customerlist; x++ )
    		{
    		int cus = (int) (Math.random() * customerlist);
    		line.add(new Customer(list[cus]));
    		}
     
    		try
    		{
    			System.out.println("Enter the number of cashiers available HERE: " + "\n");
    			int cashier = input.nextInt();
    		}
    		catch (Exception ex)
    		{
    			System.out.println("Enter a whole number you douche. " + "\n");
    			System.exit(0);
    		}
    		Random ran = new Random();
    		while (!line.isEmpty())
    		{
    			System.out.println(line  + "\n");
    			System.out.println("The queue has " + line.size() + " customers left");
    			Customer cus = line.remove();
    			System.out.println(cus.name + " queued at " + cus.getTime() + " <=== SERVED" + "\n");
    			// you will have to sleep a random number of seconds here
    			int wait = ran.nextInt(2) + 1;  // will generate 1 or 2
    			try 
    			{
    			     Thread.sleep((wait * 1000) / cashier);
    			}
    			catch(Exception e) 
    			{
    				System.out.println("Sleep error: " + e);
    			}
     
    		}
    	}
    }
    Last edited by spiderd; April 3rd, 2013 at 05:41 PM. Reason: colour

  5. #30
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Queue Simulation

    Can each customer be represented by an arrival time in a specific queue?

  6. #31
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    A random number is generated and then that number is multiple by 1000ms which is within a sleep method hence a simulating random intervals.

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    trying to catch the exception when the user enters a letter instead of a intege
    That's a completely separate problem. Don't mix it in with the rest of the code for now. Come back to it later.

    What is the array: list for? The customers should NOT be named. Give them numbers.

    Can you explain the design for the program you are trying to write? The use of the sleep() method looks like you are trying to use the clock time model. You will NOT want to sleep full seconds (*1000). Testing with 200 customers could take hours. Use ms vs sec.
    each customer be represented by
    Create a Customer class that holds all the data needed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    So instead of sleeping for 1 seconds how about 1/4 of second which is 25ms. And what do you mean the Customer class will hold all the data ? Currently ive made a Customer class which retrives the simple time and name of the customer.

    public class Customer {
        String name;
        String time;
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        // constructor
        Customer(String name) 
        {
           this.name = name;
        }
     
        String getTime() 
        {
           Calendar cal = Calendar.getInstance();
     	   time = dateFormat.format(cal.getTime());
           return time;
        }
     
    }

    Norm is there any example of this time model which you can show so I can have a better understanding of what classes I need to make how everything works together. Before I attempt coding this assignment my tutor really didn't explain and cover everything that your explaining now. Can you help understand how and finalize the design because right now ive just done every on the spot ?

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Can you get more information from your tutor about this assignment? Both solutions I've been talking about involve classes and techniques that you have not studied yet.
    instead of sleeping for 1 seconds how about 1/4 of second
    I'd scale it down farther. Try 1ms for 1 sec first. If there are problems with that try 5ms and then 10ms for 1 sec

    For the clock time model, I used the System class's method currentTimeMillis() to get the current time.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    ok ive looked up on treads which is basically running multiple classes at once. But what I don't understand is how does threads apply to my assignment of 1 queue, 10 customers getting proccesed. Such how many classes which class does what etc

  11. #36
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Queue Simulation

    Quote Originally Posted by Norm View Post
    That's a completely separate problem. Don't mix it in with the rest of the code for now. Come back to it later.

    What is the array: list for? The customers should NOT be named. Give them numbers.

    Can you explain the design for the program you are trying to write? The use of the sleep() method looks like you are trying to use the clock time model. You will NOT want to sleep full seconds (*1000). Testing with 200 customers could take hours. Use ms vs sec.

    Create a Customer class that holds all the data needed.
    The number could be the actual arrival time.

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Using the clock model: There would be one main class and a class for the customers, a class to handle the timer events. The main class would start a thread that would have a loop to generate x customers at the customer arrival rate and add them to the wait queue as they were generated, except the first n (n = number of cashiers) customers would enter a cashier processing station. When a customer enter a cashier station, start a timer to represent the time the cashier takes to process the customer. When the timer expires, the customer is removed from the cashier station and the next customer from the wait queue is move to the cashier and the customer's waiting time in queue is recorded. The processing continues until the wait queue is empty.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Queue Simulation

    oki lets me sum it to my self, there are three classes one for the main class which contains the random customer generator in intervals. The Customer class contains the actual empty priority Queue where the generated customer will be placed. Then if the cashier is empty the next customer in the Queue will be placed in the cashier and timer will start and when the timer is finished the customer will be removed and outputted.

    However what I dont understand is what is the actual cashier where the customer will be placed in, is it another empty queue allowing only one object.

    And last thing I dont understand is the timer how are you supposed to record the time the customer enters/exits the queue so you can output the difference which is the spent queuing.

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Customer class contains the actual empty priority Queue where the generated customer
    The queue would be in the main class. It would hold Customer class objects.

    What is the third class?

    what is the actual cashier where the customer will be placed
    The cashiers could be an array of Customer objects. The Customer in a slot would be the one being processed by that cashier.

    record the time the customer enters/exits the queue
    When the customer is added to the wait queue, save the current time in the Customer object.
    When the Customer is removed from the wait queue, the time in queue would be the current time minus the time when the Customer was added to the wait queue.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Queue Simulation

    oki so there are only two classes one for timer event and the other Customer class. What do you mean the cashier could be Array of Customer objects. I taught a Array is just a list of integers or Strings. Are you saying that I first create ten new customer objects and then add the new customer objects to the cashier array list.

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    the cashier could be Array of Customer objects.
    You can create an array of any datatype: primitives like int or objects like String or Customer.
    Customer[] cashiers; // define array for cashiers
    Are you saying that I first create ten new customer objects
    No. See post#22. The Customers are generated at random time intervals inside a loop in a separate Thread.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Queue Simulation

    So does this mean I can have multiple threads within my Customer class ?
    Also with the customer queue is it best do a while loop to check if the cashier slot is empty if so, then move the customer to the array.
    And what is the actual syntax to move customer from the queue to array list
    its not
    .remove() or .element()

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    have multiple threads within my Customer class
    What code would you put in a Customer? I don't have any code in mine.

    while loop to check if the cashier slot is empty
    No. The cashier slots are empty at the start of the simulation and then become available when the processing for a Customer is finished. The timer listener method is called when the processing is done. At that time in the listener method, the next Customer could be moved from the wait queue to the empty cashier.

    syntax to move customer from the queue
    Read the API doc for the class to see what method can be used to remove an element from the list.

    I suggested in post#22 that the code be written in steps. The first step would be the thread with a loop to generate n Customer objects at random times, put them in a queue and then when the loop exits, print out the contents of the queue to see what values for times were generated.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Queue Simulation

    Quote Originally Posted by Starstreak View Post
    Can each customer be represented by an arrival time in a specific queue?
    What do you mean ?

  20. #45
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    oki this is what ive done so far ive generated maximum of 10 customer object arriving randomly in the queue between 1-4 sec.

    public class Customer implements Runnable
    {
    	public static Queue<String> line = new  LinkedList<String> ();
    	public void run() 
    	{
    		String[] list = {"Customer A", "Customer B", "Customer C", "Customer D", "Customer E", "Customer F", "Customer G", "Customer H", "Customer I", "Customer J", };
    		int customerlist = list.length;
     
    		Random time = new Random();
     
     
    		for (int i = 0; i < customerlist; i++)
    		{
    			int cus = (int) (Math.random() * customerlist);
    			int wait = time.nextInt(4) + 1;
     
    			try
    			{
    				try 
    				{
    					Thread.sleep(wait * 1000);
    					line.add(new String(list[cus]));;
    				} 
    				catch (InterruptedException e) 
    				{
     
    					e.printStackTrace();
    				}
     
    			}
    			finally
    			{
    				System.out.println(line);
    			}
     
    	Customer[] cashiers;
     
     
    		}
     
    	}

    The only real time I want to record and output is the time customer spent queuing, so this means I need to record the time the customers enter the queue and exit the queue, how do i do this Norm ? Also Ive decided that when a customer moves to the cashier it take 3 seconds for the customer to get processed and moved to a queue called processed customers. This would then allow the existing customer to wait in the queue and then I can output the time spend waiting.

    However then I have the problem of use can input how many cashier exist, I have hit a wall an ideas ?

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    ive generated maximum of 10 customer
    I thought it was supposed to be a minimum of 10.

    Where is the class to hold the Customer's data? Things like time of arrival in queue and what cashier is doing the processsing and how much time was spent in the wait queue?

    The posted code looks like what would go in the Main class. Where are the Customer objects created and added to the wait queue? This code is adding Strings??? The Customer objects saved on the queue need the data mentioned in the first part of this post.

    You do NOT want to wait seconds. Use scaled down values for the times: 1ms for 1 sec (See earlier posts on this)

    you do NOT need the String array: list

    I need to record the time the customers enter the queue and exit the queue
    You need the fields in the Customer class to do that.

    Define a Customer class and work on filling the wait queue with Customer objects with the time they entered the queue saved in them. Make a loop that creates n Customer objects, and puts them in the queue with the time set.
    After n are generated, print out the queue's contents so you can verify that the generate step worked correctly.


    Timing values seem way to quick:
    arriving randomly in the queue between 1-4 sec. <<<<< seems very short times
    take 3 seconds for the customer to get processed <<< WAY TOO SHORT
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    Member
    Join Date
    Apr 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Queue Simulation

    I just need to simulate 10 customer apparently and the reason why Im working in seconds is because at the end im going to print out each stage of the so it actually looks like a real queue building up and get processed. I taught that when you meant make a loop creating Customer objects it was the same as passing a String to the queue.

    Is this correct ive in a loop im adding new Customer objects at a random time. and scaled down to 1ms to 1sec.
    public class Customer implements Runnable
    {
    	public static Queue<Customer> line = new  LinkedList<Customer> ();
    	public void run() 
    	{
    		Random time = new Random();
    		for (int i = 0; i < 10; i++)
    		{
    			int wait = time.nextInt(4) + 1;
     
    				try 
    				{
    					Thread.sleep(wait * 1);
    					line.add(new Customer());
    				} 
    				catch (InterruptedException e) 
    				{
     
    					e.printStackTrace();
    				}
    				finally
    				{
    				System.out.println(line);
    				}
    		}
            Customer[] cashiers;
     
    	}

    Its printing out this:
    [Customer@8955b34]
    [Customer@8955b34, Customer@596b753]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df, Customer@69912a56]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df, Customer@69912a56, Customer@3972aa3f]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df, Customer@69912a56, Customer@3972aa3f, Customer@17072b90]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df, Customer@69912a56, Customer@3972aa3f, Customer@17072b90, Customer@57bd06bf]
    [Customer@8955b34, Customer@596b753, Customer@69ddad02, Customer@5889949a, Customer@307b37df, Customer@69912a56, Customer@3972aa3f, Customer@17072b90, Customer@57bd06bf, Customer@3b061299]

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    Now you need to add code to save the time the Customer object is added to the queue.

    And add a toString() method to the Customer class that returns the id number for the customer and the time the object was added to the queue. The String that the toString() method returns will replace this: Customer@69ddad02


    Don't bother to print out the contents of the queue until all Customers have been added to it.

    There should be some variables at the very beginning of the class that define:
    the number of customers to generate (don't hardcode a 10 in the for loop)
    the max time between customer arrivals
    the number of cashiers
    and some others

    The class's name should be something besides Customer. There should be a small inner class named Customer that has several data fields such as an id, the time entered queue, the cashier doing the processing and the time spent waiting in queue. Some of those will be given values later.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Queue Simulation

    Im trying to add the .toString()
    line.add(new Customer().toString());
    but underlining the .add so I believe I'm adding it wrong. Also Ive added variables as you described to the very begning of the class:

    Scanner input = new Scanner (System.in);
    	int cashier = input.nextInt();
    	private int max_customers = 10 - 20;
    	private int max_arrival = 2; // 2 sec
     
    	private class Customer
    	{
    		int id;
    		int arrival_time;
    		String cashier;
    		int queue_time;
    	}

    But I was thinking is it better that for int max_arrival; and int queue_time; I change the data type to Time ?

  25. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Queue Simulation

    The toString() method's DEFINITION is added to the Customer class. You don't call the method.
    The println() of the line object will call the toString() method and change this: [Customer@8955b34]
    to the String the toString() method returns:
    	private class Customer
    	{
    		int id;
    		int arrival_time;
    		String cashier;
    		int queue_time;
     
                   public String toString() {
                       return "Customer id="+id + " arvTm="+arrival_time;
                   }
    	}
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 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, 05:51 PM