Make a package PQ, and within it, place class PriorityQueue. The method must satisfy this API specification exactly as it is written:
/** class PriorityQueue implements a priority queue, in which objects are stored with their priority numbers. */ public class PriorityQueue /** method insert places a new object into the queue * @param num - the object's priority number; it must be a positive * number. (The lower the number, the higher the priority --- * 1 is highest, then 2, etc.) * @param ob - the object stored in the queue * @throw RuntimeException if num is not a positive number or if * ob has value null. */ public void insert(int num, Object ob) /** method retrieve removes from the queue the highest priority object. * If there are multiple objects that both have the highest priority, then * the object that has resided in the queue for the longest time is * retrieved. * @return the (address of) the object retrieved * @throw RuntimeException if the priority queue is empty */ public Object retrieve() /** function isEmpty checks whether the queue is empty. * @return true, if the queue is empty; otherwise, return false. */ public boolean isEmpty() /** method toString returns a string representation of the contents of the * queue. (This is useful for debugging purposes.) * @return a string that lists the objects and their priority numbers, * enclosed by square brackets. For example, if the queue holds * ob A with priority 5 and ob B with priority 3, the string would say * "[5,A 3,B]". * (But the order in which the items are listed in the string is unimportant. * so "[3,B 5,A]" is OK also. If the queue is empty, "[]" is returned.) * Note: for each object, ob, in the queue, ob.toString() is * used to make the string that represents ob in the answer string. */ public String toString()Within package PriorityQueue, you should also include the ``helper classes'' needed to implement the queue. (These will be the classes for building leaves and nodes of binary trees.) You will probably find it useful to reuse package BitList, from Assignment 5, to count the nodes and leaves of the heap structure when inserting and retrieving objects. You can find package BitList at http://www.cis.ksu.edu/~schmidt/300f04/Assign/Assn5/.
Test your implementation with one or more test programs, like this one:
import PQ.*;
/** Test gives a script for testing a priority queue */
public class Test
{ public static void main(String[] a)
{ PriorityQueue q = new PriorityQueue();
System.out.println(q.toString()); // prints []
if ( q.isEmpty() )
{ System.out.println("OK so far"); }
q.insert(8, "abc");
q.insert(3, "def");
q.insert(4, "ghi");
if ( q.isEmpty() )
{ System.out.println("wrong answer"); }
else { System.out.println(q.toString()); } // prints [8,abc 3,def 4,ghi]
String s = (String)(q.retrieve()); // "def" is returned
System.out.println(s); // prints def
q.insert(4, "jkl");
System.out.println(q.toString()); // prints [8,abc 4,ghi 4,jkl]
System.out.println(q.retrieve().toString()); // prints ghi
System.out.println(q.toString()); // prints [8,abc 4,jkl]
// An experiment: make a second queue and insert a string into it:
PriorityQueue r = new PriorityQueue().insert(1, "strange");
q.insert(2, r); // it's strange but legal
System.out.println(q.toString()); // prints [8,abc 4,jkl, 2,[1,strange]]
// now, retrieve r and print it:
System.out.println(q.retrieve().toString()); // what does it print?
q.retrieve(); // "jkl" is returned
System.out.println(q.toString()); // prints [8,abc]
}
}
When the Teaching Assistant receives your submission, he will insert
it into similar test programs and verify that your implementation
meets all the standards stated in the above API specification.
Implementation and submission requirements:
Make a folder named Assign7. Within it, place package PQ and any other packages it uses. Document all classes and package them accordingly, and run javadoc on your completed program. Submit Assign7.jar at http://www.cis.ksu.edu/~schmidt/300f04/Assign/submit.html.