Problem

Assume an ordinary queue has been declared with the Queue interface and instantiated with...

Assume an ordinary queue has been declared with the

Queue
interface and instantiated with the
ArrayDeque
class. Also, assume a priority queue has been declared with the
Queue
interface and instantiated with the
PriorityQueue
class. Figure showed that the
PriorityQueue
class also implements the
Queue
interface. Therefore, the
PriorityQueue
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 priority queue inserts them into the queue so that lower-valued elements are always closer to the front. In other words, in a priority queue, lower-valued elements have priority and “crash” the line. Given this understanding, show the output produced by the following code fragment:

ordinaryQueue.add(8);ordinaryQueue.add(3);ordinaryQueue.add(12);System.out.println(ordinaryQueue.remove());ordinaryQueue.add(5);while (!ordinaryQueue.isEmpty()){    priorityQueue.add(ordinaryQueue.remove());}while (!priorityQueue.isEmpty()){    System.out.println(priorityQueue.remove());}

You should be able to do this by just thinking about it, but feel free to complete and run a program to check your answer.

Figure Part of the top of the Java collections framework interface hierarchy

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 10
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT