Bill Teter
Office:          149 Redcay
Telephone:    2782
Office Hours:  Tuesday, Thursday 9:00-12:00

email:  william.teter@plattsburgh.edu

       

 

Lab 12 (lab exam)  Sorting

Problem:  A text data file contains information on people:  name and state of residence.  Your task is to list the people in sorted order in two different ways.  First print in sorted order by name.   Second print them in sorted order by state and if two people come from the same state, then use their names to determine order.  The datafile is called people.  Each line of the datafile contains a name and state of residence separated by a tab character.  You can use the unix utility wc to determine how many lines are  in the file so that you can declare an array of exactly the right size.

Procedure.

1.  Create a Person class where a person has a name and state of residence.  This class should implement Comparable<Person> using the name field with the string compareTo() method.  Include an appropriate toString() method.

2.  Create a main program that reads the text file a line at a time, splits each line on the tab character (use split("\\t")), creates a person object and stores that object in a Person[] array of the appropriate dimension.

3.  Create a CompareByState class that implements Comparator<Person> using the state of residence field.

4.  The main program should sort the person array using the natural ordering (by name) and then sort the array again using a CompareByState comparator.  Since the sorting routine in the Arrays class is stable the array should now be properly sorted for the second ordering.