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

email:  william.teter@plattsburgh.edu

       

 

lab 8 (lab exam)  Comparators

In this lab you will modify the SortedArrayList class so that it will work with a comparator as well as with the compareTo() method.  We discussed how to do this in class and the basic steps are outlined below.

step 1.  Create in your 223_wc repository a new directory called lab8.

step 2.  Update all the new files in my repository for lab8 and copy them into your new lab8 directory. (I have a working version of a SortedArrayList class or you can use your own from lab7.)

step 3.  Add a constructor to the SortedArrayList class with a parameter that is a Comparator<AT>.  Assign the parameter to a new Comparator<AT> field called cmp.  In this class you can use compareTo() if cmp is null, or otherwise use the cmp.compare() method.

step 4.  I suggest you write a new private method called doCompare(AT l, AT r) that returns an int and uses either compareTo() or cmp.compare() depending on whether or not cmp is null.  Then, replace all occurrences of compareTo() calls with appropriate calls to doCompare().

step 5.  Test your program by completing the Test program.  The test involves another class called Person.  A Person object has a name, age, and height with accessor methods for age and heigh and a toString() methodl. Your test program reads in a data file called "people" that consists of the name, age and height of people, each field separated by a tab. The test program creates 3 sorted array lists sorting by name, age and weight respectively. The Test program will then print out each of these arrays.  What you need to provide is 2 classes that implement the comparator interface for Person--one using the age of the person the other the height.  Call these classes CompareByAge and CompareByHeight.