THINK FIRST·CODE LATER

← All labs

Union, difference and intersection of two priority queues

Problem

Write QueueSets: read two lines of words and put each line in a PriorityQueue<String>. Print:

  1. the first queue as it is stored (toString) and then in priority order — they differ!
  2. the second queue in priority order;
  3. their union (no duplicates), their difference (first without second) and their intersection, each as a new priority queue, in priority order;
  4. the size and the head of the first queue, to show that it was not modified.

Write a method inOrder(PriorityQueue<String>) that returns the elements in priority order without emptying the queue it receives. For the input

George Jim John Blake Kevin Michael
George Katie Kevin Michelle Ryan

the program prints

First, as stored: [Blake, George, John, Jim, Kevin, Michael]
First, in priority order: [Blake, George, Jim, John, Kevin, Michael]
Second, in priority order: [George, Katie, Kevin, Michelle, Ryan]
Union: [Blake, George, Jim, John, Katie, Kevin, Michael, Michelle, Ryan]
Difference (first without second): [Blake, Jim, John, Michael]
Intersection: [George, Kevin]
First still has 6 element(s); its head is Blake

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d11-9-data-1.txt

George Jim John Blake Kevin Michael
George Katie Kevin Michelle Ryan
What your program must print for d11-9-data-1.txt
First, as stored: [Blake, George, John, Jim, Kevin, Michael]
First, in priority order: [Blake, George, Jim, John, Kevin, Michael]
Second, in priority order: [George, Katie, Kevin, Michelle, Ryan]
Union: [Blake, George, Jim, John, Katie, Kevin, Michael, Michelle, Ryan]
Difference (first without second): [Blake, Jim, John, Michael]
Intersection: [George, Kevin]
First still has 6 element(s); its head is Blake

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d11-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content d11-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.