Not every detail is spelled out for you in the problem descriptions. You will need to read the Java Documentation to fill in some of the details. You will need to be able to apply the various ways we managed abstraction, as discussed over several lectures.
You will have two main tasks in this assignment. The first is to create an
Algorithms class of useful methods, described below. The second
is to provide an implementation of a Priority Queue (subject to the
restrictions outlined below) that extends the Java Collections Framework.
For the second task you should make a concerted effort to write as little
original code as possible, but still satisfy the restrictions given for the
design of the Priority Queue. Remember, the idea of the Java Collections
Framework is to provide you with a set of already-written (and tested!) methods
that you may use as is (or override if necessary).
Algorithms. Your Algorithms
class must provide methods for doing a Selection Sort on an ArrayList. The
Algorithms class will also provide a method for inserting an item
into a sorted ArrayList.
When the program processes the list of data for the first time, it finds the location of the smallest item in the list. It then swaps the first item in the list with the smallest one (even if the smallest one is already in the first spot).
Next time around, it does the same, but only with the rest of the list, i.e. all items beyond the first one. The third time around, it starts with the third item, because the first two are already in the correct places, and so on.
This is hard to do with recursively constructed lists, but is much easier when we can directly swap two items at specific locations, as is the case when the data is stored in an ArrayList.
In the Algorithms class design a method called SelectionSort
that consumes an ArrayList<T> and an instance of a class that implements
Comparator<T> and mutates the ArrayList<T> so that it is sorted in the order given by the Comparator<T>.
It is possible to combine all parts of the Selection Sort algorithm into one method, but that is not a good way to design programs. Your program should use the following helper methods:
swap() that swaps the elements at the two given index positions in the given ArrayList<T>
findMinLoc() that finds in the given ArrayList<T> the
index of the minimum element among all elements, at the index greater than or equal to the given index. Of course, it also consumes the Comparator<T>.
selectionSort() that implements the algorithm that is described above
Algorithms class a void method called insertIntoSortedList(). This method
consumes an ArrayList<T> that is already sorted (according to
the given Comparator<T>), an element of type T to insert
into the ArrayList, and a Comparator<T>. The method inserts
the new element into the ArrayList in the proper position, maintaining the
list in sorted order. (You may use this method when implementing your
Priority Queue, in Part 2).
Algorithms class. Make a simple class of data, such as a Book or Song or something of your own choosing. Define two different Comparators for this class. Then make examples of ArrayLists of these data items and make sure your JUnit tests use both of the Comparators.
Design a class called PriorityQueue<T>. Your PriorityQueue class must directly implement AbstractCollection<T>.
The underlying data structure for your Priority Queue should be an ArrayList<T> (i.e. the elements of the Priority Queue should be stored in
an ArrayList). The ArrayList should be maintained in sorted order so that
the item with the highest priority will always be the first (zeroth) item in
the ArrayList. The Priority Queue should provide the following methods:
// retrieve and remove the item at the head of this priority queue
// (i.e. the item with the highest priority)
// throw NoSuchElementException if the priority queue is empty
T remove();
// add the given element to this priority queue
boolean add (T t);
// retrieve, but do not remove, the item at the head of this priority queue
// (the item retrieved will be the item with the highest priority)
// throw NoSuchElementException if the priority queue is empty
T peek();
// returns true if this priority queue is empty
boolean isEmpty();
Of course, the Priority Queue class must also provide implementations for all
of the obligations it inherits.
Construct JUnit tests for each method defined in your PriorityQueue class.
Provide an Examples class that defines a main method that demonstrates
the functionality of your PriorityQueue class.
You should create examples of priority queues composed of Books, Songs, or
whatever your
favorite class is. Create at least two different priority queues, each
prioritized according to different Comparators. Your main method should
produce neatly-labelled output that shows the contents of the priority
queues after adding elements, removing elements, etc.
Using web-based turnin, turn in a single file hw7.username.zip containing all code and documentation for this assignment. Both partners' names and wpi login names should appear in a comment at the top of the file.