Queue Behavior *Programming*

profileieas2010

Queue Behavior :

 

Write a program that shows what happens when random values go first through an ordinary queue and then through a priority queue. The Queue interface specifies add and remove methods that add elements to and remove elements from a queue. Figure 10.10 shows that when the queue is an instance of the ArrayDeque class, the add method adds elements to the back of the queue and removes elements from the front of the queue. Figure 10.15 shows that another class, PriorityQueue, also implements the Queue interface. Therefore, this other class also implements add and remove methods. Its remove method does the same thing as ArrayDeque’s remove method. But its add method is different. When it adds elements, a PriorityQueue inserts them into the queue so that lower-valued elements are always closer to the front. In other words, lower valued elements have priority and “crash” the line. In an initial for loop, generate five random double values and add them to an ordinary queue. In a subsequent while loop, remove all elements from that ordinary queue and add them to a priority queue. In a final while loop, remove all elements from that priority queue. Display the values going into the ordinary queue, coming out of the ordinary queue and going into the priority queue, and coming out of the priority queue, like this:

Sample session:

Input to ordinary queue

 0.257

 0.804

 0.118

 0.280

 0.199

Output from ordinary queue and input to priority queue

 0.257

 0.804

 0.118

 0.280

 0.199

Output from subsequent priority queue

 0.118

 0.199

 0.257

 0.280

 0.804

Random number generation will make your values different from these, but the ordering should be similar.

 

 

This is information of the book if needed.

ISBN:

007337606X


ISBN-13:

9780073376066


Authors:
    • 6 years ago
    • 15
    Answer(1)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      package_queue.docx
    Bids(1)