The problem is to simulate the operation of a printer that prints the jobs accor
ID: 674358 • Letter: T
Question
The problem is to simulate the operation of a printer that prints the jobs according to priority. The priority of a job is determined by two factors: user_priority: an integer from 1 to 3 (1 is highest) numpages: pages to be printed Job priority = user_priority * numpages For example, consider two users: Joe: user_priority = 3, numpages=50 Sue: user_priority = 1, numpages=10 Joe's priority = 3x50 = 150 Sue's priority = 1x10 = 10 Since Sue has a high user-priority and is only printing 10 pages, she will have priority over Joe. A job to print should be represented by a class named Printjob. This class should contain the user's name, the user's priority, and number of pages. It should implement Comparable with compareTo based on these factors. Derive a subclass of Printjob called OutsidePrintjob. These are just like Printjobs, but they compute a cost based on 10 cents per page. Another class called Printer should read an input file and create objects for each entry. These objects should be added to a priority queue using the author's BinaryHeap class (unmodified). The input file contains each job to print on a separate line, with tabs between the fields. The fields are name, user priority, pages, and a flag indicating inside or outside job (I or O). Once the file is read and the print jobs have been added to the binary heap, the Printer object should deleteMin each job and print its user's name, priority, and pages to the screen. OutsidePrintjobs should also show their cost
Explanation / Answer
public class BinaryHeap implements PriorityQueue { /** * Construct the binary heap. */ public BinaryHeap( ) { currentSize = 0; array = new Comparable[ DEFAULT_CAPACITY + 1 ]; } /** * Construct the binary heap from an array. * @param items the inital items in the binary heap. */ public BinaryHeap( Comparable [ ] items ) { currentSize = items.length; array = new Comparable[ items.length + 1 ]; for( int i = 0; i 0; i-- ) percolateDown( i ); } /** * Test if the priority queue is logically empty. * @return true if empty, false otherwise. */ public boolean isEmpty( ) { return currentSize == 0; } /** * Returns size. * @return current size. */ public int size( ) { return currentSize; } /** * Make the priority queue logically empty. */ public void makeEmpty( ) { currentSize = 0; } private static final int DEFAULT_CAPACITY = 100; private int currentSize; // Number of elements in heap private Comparable [ ] array; // The heap array /** * Internal method to percolate down in the heap. * @param hole the index at which the percolate begins. */ private void percolateDown( int hole ) { int child; Comparable tmp = array[ hole ]; for( ; hole * 2Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.