Comparator Class Exercise


Use eclipse for this exercise.

  1. Copy all files from Shared Files, Comparator Exercise folder. You will also need your Person class and your CollectPerson class from the Iterator exercise. If you don't have them, copy PersonSolution and CollectPersonSolution from the Collections exercise folder.
  2. The Collection in main should be changed to a SortedABList of Person objects.
  3. Add field for social security number to the Person class. Adjust the existing Person methods to include this field.
  4. Make the Person class Comparable and create a compareTo method. The compareTo method should use the last name and first name. Compare the last names and if they are equal compare the first names.
  5. Keep the loop at the end of main to print the list. The list class already includes an Iterator so you don't need to write one.
  6. Run your program, and check that the Persons print in alphabetical order at the end of the program.
  7. Add a Comparator to the Person class which compares two Persons based on SSN.
  8. Change main so that it creates the SortedABList using the comparator you just wrote. Instead of prompting for first and last names, prompt for the SSN when searching for a Person in the list.
  9. Modify build function in client code to read social security number along with other fields. Modify the addPerson function parms and to prompt and read first and last names for the person being added.
  10. Run your program and make sure it works. The Persons should now print in SSN order.
  11. Add another Comparator to the Person class which compares two Persons based on age.
  12. Change main so that it creates the SortedABList using the comparator you just wrote. Instead of prompting for SSN, prompt for the age when searching for a Person in the list.
  13. Check your program again.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2017