For this lab you will write a program that reads integers from standard input and keeps the smallest N of them. I'll supply the shell of the program. I'll also supply HeapMaxPQ and HeapMinPQ, which are implementations of the MinPQ and MaxPQ interfaces. You'll write the rest. What you'll need to do is this: 1. Create a Scanner object to read from standard input (System.in). 2. Read N (the number of smallest items to keep) from standard input. 3. Create a priority queue (either HeapMaxPQ or HeapMinPQ - you'll need to figure out which). 4. read integers from standard input until there are no more to read. 5. Use the priority queue to keep track of the smallest N integers. 6. When you're done reading the data, write out the N smallest integers from largest to smallest, one per line. For example, if you're keeping the 4 smallest integers, and the input data is 3 19 81 9 18 58 40 61 13 74 82 60 64 45 40 47 24 60 47 90 the output should be 18 13 9 3 Recall that the API for MinPQ is: Function constructor insert min del Min size isEmpty Signature HeapMinPQ(int initialSize) void insert(Tt) T min() T delMin() int size() boolean isEmpty() constructor Description inserts t into the priority queue returns the current minimum value without removing it returns the current minimum value and removes it returns the number of items in the priority queue returns true if the priority queue is empty, false otherwise The API for MaxPQ is similar, but with Max substituted for Min. You'll need to figure out how to use the member functions of MinPO or MaxPO to do the tasks outlined above.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help with the main function, please.

I have all classes I need I just need the main function

For this lab you will write a program that reads integers from standard input and keeps the smallest N of them. I'll supply the shell of
the program. I'll also supply HeapMaxPQ and HeapMinPQ, which are implementations of the MinPQ and MaxPQ interfaces. You'll write the
rest.
What you'll need to do is this:
1. Create a Scanner object to read from standard input (System.in).
2. Read N (the number of smallest items to keep) from standard input.
3. Create a priority queue (either HeapMaxPQ or HeapMinPQ - you'll need to figure out which).
4. read integers from standard input until there are no more to read.
5. Use the priority queue to keep track of the smallest N integers.
6. When you're done reading the data, write out the N smallest integers from largest to smallest, one per line.
For example, if you're keeping the 4 smallest integers, and the input data is
3 19 81 9 18
58 40 61 13 74
82 60 64 45 40
47 24 60 47 90
the output should be
18
13
9
3
Recall that the API for MinPQ is:
Function
constructor
insert
min
del Min
size
isEmpty
Signature
HeapMinPQ(int initialSize)
void insert(Tt)
T min()
T delMin()
int size()
boolean isEmpty()
Description
constructor
inserts t into the priority queue
returns the current minimum value without removing it
returns the current minimum value and removes it
returns the number of items in the priority queue
returns true if the priority queue is empty, false otherwise
The API for MaxPQ is similar, but with Max substituted for Min.
You'll need to figure out how to use the member functions of MinPQ or MaxPQ to do the tasks outlined above.
Transcribed Image Text:For this lab you will write a program that reads integers from standard input and keeps the smallest N of them. I'll supply the shell of the program. I'll also supply HeapMaxPQ and HeapMinPQ, which are implementations of the MinPQ and MaxPQ interfaces. You'll write the rest. What you'll need to do is this: 1. Create a Scanner object to read from standard input (System.in). 2. Read N (the number of smallest items to keep) from standard input. 3. Create a priority queue (either HeapMaxPQ or HeapMinPQ - you'll need to figure out which). 4. read integers from standard input until there are no more to read. 5. Use the priority queue to keep track of the smallest N integers. 6. When you're done reading the data, write out the N smallest integers from largest to smallest, one per line. For example, if you're keeping the 4 smallest integers, and the input data is 3 19 81 9 18 58 40 61 13 74 82 60 64 45 40 47 24 60 47 90 the output should be 18 13 9 3 Recall that the API for MinPQ is: Function constructor insert min del Min size isEmpty Signature HeapMinPQ(int initialSize) void insert(Tt) T min() T delMin() int size() boolean isEmpty() Description constructor inserts t into the priority queue returns the current minimum value without removing it returns the current minimum value and removes it returns the number of items in the priority queue returns true if the priority queue is empty, false otherwise The API for MaxPQ is similar, but with Max substituted for Min. You'll need to figure out how to use the member functions of MinPQ or MaxPQ to do the tasks outlined above.
You'll need to figure out how to use the member functions of MinPQ or MaxPQ to do the tasks outlined above.
The provided implementations of HeapMinPQ and HeapMaxPQ will resize the PQ as needed as you add items. However, if you do this
correctly, you should be able to specify an initial size to the constructor that will be exactly what you need so that the PQ never gets
resized.
Important: Your program may NOT keep more than N+1 items in the priority queue, where N is the command-line parameter that
determines how many values are kept.
It is possible that there are fewer than N input items. This affects the way you produce the final output.
NOTES:
1. You can put everything in main(). This is a program, not an ADT.
2. You do NOT have to implement your own priority queue. I'm supplying HeapMinPQ and HeapMaxPQ.
3. Unlike the LastN lab, here you are allowed to destroy the PQ as your write the final results.
4. The fact that you are allowed to store N+1 items may be a hint!
Transcribed Image Text:You'll need to figure out how to use the member functions of MinPQ or MaxPQ to do the tasks outlined above. The provided implementations of HeapMinPQ and HeapMaxPQ will resize the PQ as needed as you add items. However, if you do this correctly, you should be able to specify an initial size to the constructor that will be exactly what you need so that the PQ never gets resized. Important: Your program may NOT keep more than N+1 items in the priority queue, where N is the command-line parameter that determines how many values are kept. It is possible that there are fewer than N input items. This affects the way you produce the final output. NOTES: 1. You can put everything in main(). This is a program, not an ADT. 2. You do NOT have to implement your own priority queue. I'm supplying HeapMinPQ and HeapMaxPQ. 3. Unlike the LastN lab, here you are allowed to destroy the PQ as your write the final results. 4. The fact that you are allowed to store N+1 items may be a hint!
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education