next up previous
Next: About this document ... Up: Assignment 4: Exam Recovery Previous: Lists

Priority Queue

Assume that you have an implementation of a Priority Queue that provides the usual set of methods with the usual behavior:

public class PriorityQueue {
  public PriorityQueue ();
  public int size ();
  public void insert (PQItem newItem);
  public PQItem remove ();
}

While working on another program, it occurs to you that it would be useful to have a new method called removeNthElement that would take a number n as an argument and would remove and return the element with the nth highest priority.

You have a choice between two approaches:

internal:
you can modify the file PriorityQueue.java and add the new method to the class definition. In this approach, you can access the instance variables as well as the other methods.

external:
you can build the new method externally (in another file), using only the publicly-accessible methods and not the instance variables.

1.
Using any of the implementations we have seen so far, write an external implementation of removeNthElement. Even thought it is an external method in the sense defined above, you can write it inside PriortityQueue.java. For the sake of clarity, though, you should put it down at the bottom with main. Also, make it a class method, so it takes the queue and n as arguments.

2.
Add code to main to test your program.

3.
Copy your code from this implementation into another and confirm that it still works, without modification.

4.
Turn in a copy of this method only, along with any comments about your experience.


next up previous
Next: About this document ... Up: Assignment 4: Exam Recovery Previous: Lists
Allen B. Downey
1998-10-07