next up previous
Next: What to turn in Up: Assignment 7: Strings and Previous: Palindromes

Names

When names are recorded in a computer, they are sometimes written with the first name first, like ``Allen Downey,'' and sometimes with the last name first and a comma, like ``Downey, Allen.'' That can make it difficult to compare names and put them in alphabetical order.

A related problem is that some names contain capital letters in funny places. When computers compare characters, they usually put all the capital letters before the lower-case letters. We discussed the reasons for that in class, but the consequence is that computers often put names with capitals in the wrong order.

A good solution to this problem is to convert all names to a common format so that they can be compared using the built-in compareTo method. In this homework we will do that conversion. Later we will use this technique to sort a list of names.

1.
Create a new project called Name.prj based on the cs151HelloStationery.

2.
Write a method called hasComma that takes a name as an argument and that returns a boolean indicating whether it contains a comma. If it does, you can assume that it is in last name first format.

3.
Write a method called convertName that takes a name as an argument. It should check whether it contains a comma. If it does, it should just return the string.

If not, then it should assume that the name is in first name first format, and it should return a new string that contains the name converted to last name first format.

4.
Write a method called compareName that takes two names as arguments and that returns -1 if the first comes before the second alphabetically, 0 if the names are equal alphabetically, and 1 otherwise.

You will want to use the compareTo method, so you should read the documentation of that method carefully. Test it out with some simple cases to be sure you understand how it works.


next up previous
Next: What to turn in Up: Assignment 7: Strings and Previous: Palindromes
Allen B. Downey
1999-10-19