CPSC 120A Fall 2002
Program 3: Naming Names
Due
Monday, November 18
Introduction
One problem with asking for user input is that you may not get it
in the form you want. For example, if you ask the user to enter a
date there are a number of formats that he or she might choose
(Month Day Year, mm/dd/yy, mm-dd-yyyy, etc). Asking for a full
name seems safer; you'd expect to get something like
"Mary Sue Jones" or "Michael John Smith." But you might
get something like "mary jones" or "Michael john Smith." This
can make the input difficult to process and can interfere with producing
clean-looking output.
To
remedy this, programs often put input into some kind of
normal form. For example, normal form for a date might
be mm/dd/yyyy format. Normal form for a name might be
First Middle Last. In this program you will put names into normal form.
Program Description
Write a Name class with the following public methods:
- Name(String s) -- Constructor. Takes the name in unnormalized form and
normalizes it.
- String toString() -- returns the full name
- String initials() -- returns the initials in all capitals
- void setName(String s) -- sets the full name to be s as given; does
not normalize it.
In normal form the name should have the following properties:
- Exactly one space between parts (e.g., first and middle names)
- No leading or trailing spaces
- Each part starts with a capital letter with the remaining letters
in lower case.
- Initials should be in all capitals and should contain no spaces.
Note that normal form is not always appropriate, e.g.,
McCarthy will become Mccarthy. For these cases, the
setName method allows the user to override
the normalization. Calling the setName method should not change
the initials -- they should be determined when the name is first created
(in the constructor).
Also note that the name given to the constructor
may have any number of parts, e.g., Sue,
Sue Brown, Sue Ellen Brown, Sue Ellen Smith Brown, and so on.
You must deal with any length name effectively.
To test your Name class, write a driver that asks the user
to enter a name, then prints a menu of options allowing the user to
see the full name, see the initials, set the name (overriding the
normalization), or quit.
Don't print the menu every time, but be sure the user knows how to get
it if he or she needs it.
For example, a run of your program
might look like this:
** Welcome to the name program!! **
Enter your full name: MARY sue MCCarthy
** Options: **
1: Show full name
2: Show initials
3: Set name (won't be normalized)
4: Quit
Enter choice (0 to see menu): 1
Mary Sue Mccarthy
Enter choice (0 to see menu): 2
MSM
Enter choice (0 to see menu):3
Enter new name: mary sue McCarthy
Enter choice (0 to see menu): 0
** Options: **
1: Show full name
2: Show initials
3: Set name (won't be normalized)
4: Quit
Enter choice (0 to see menu): 1
mary sue McCarthy
Enter choice (0 to see menu): 2
MSM
Enter choice (0 to see menu): 4
Bye!
Program Structure
Use a StringTokenizer to find the parts of the name the
user enters. Call private methods as necessary to get the work
done -- your program should be cleanly broken into pieces. We will
discuss program structure in more detail in class.
What to Turn In
Turn in hardcopy of your Names.java and TestNames.java programs and
tar up the directory and email it to bloss@roanoke.edu. Put
cpsc120 prog3 in the subject line.