![]() |
CS 112 - Introduction to Computer Science II
Homework #5 |
NOTE: This work is to be done in pairs. This means that each team will perform one submission with all team names and studentIDs listed in the README file after the honor pledge. Although team members can divide work, each team member should be able to informally present all work of his/her teammate.
1. Comparable Merge Sort: Copy Horstmann's merge sort code and modify it so that it sorts arrays of objects implementing the Comparable interface. That is, the constructor should now take a single Comparable[] parameter. For each Horstmann file that is to be modified, name the new file with Comparable at the beginning of the filename (e.g. MergeSorter becomes ComparableMergeSorter). Modify test and timing code accordingly to demonstrate the correctness of your code. For simplicity in testing and timing, I recommend that you sort Integer objects, which implement the Comparable<Integer> interface. Alternative Exercise: Do the same modifications with the quicksort algorithm (Advanced Topic 19.3).
2. CS 112 Phone Directory: You will be implementing a phone directory program for CS 112. Your program should be called Directory.java. Your program will read its directory information from the standard input. On each line of the input, a single directory record will be supplied in the following form:
<first name> <middle name> <last name> <phone number> <PO box>
For example,
Todd William Neller 337-6643 0402
Define a class Record and create a Record object to hold information for each record of the directory. That is, you'll be reading in each line, and creating a Record object to hold a person's first, middle, and last names, phone number, and PO box. The Record class should implement the parameterized Comparable interface (Advanced Topic 19.4) such that it can be sorted by (1) last name, then (2) first name, and finally (3) middle name. Define a toString method for Record such that the example record above would be have the following String representation:
Neller, Todd William
337-6643
Campus Box 0402
Put each record into an ArrayList<Record>. When the file has been completely read, you can get a Record[] of the elements using a very handy method of ArrayList (see the Java Package Documentation). Sort this array using your Comparable O(nlog(n)) sort of the previous exercise, and print the sorted phone directory using your toString method.