CSc 221 Spring 2007 Homework 2

Computer Science 221: Software Design Lab

Fall 2005

Professor McCracken

Homework 2, due before the beginning of class Tuesday, February 27, 2007

Modify your HW1 solution to do the following additional things:

  1. Add labels and textfields for seven additional items: phone number, street address, city, and state, and three groups of digits of the Social Security Number. Error checking should be done using regular expressions wherever possible. That’s seven textfields and five labels, since the three textfields of the Social Security Number require only one label.
    1. The phone number must be ten digits, and may be in either the 2223334444 form, or 222-333-4444. You need not give instructions; most people will do the second form, I think. Accept either, without comment. Reject any entry has any non-numeric character except the hyphen. Further, the first digit in the first group (222) cannot be zero or one, and first digit in the second group (333) cannot be zero.
    2. The city may be from one to four words; the state must be one or two words. (There is no need to check whether the state supplied really exists.) (There is at least one four-word city name: North Salt Lake City, Utah.)
    3. Read the street address as a string (just like the others) and do no error checking on it.
    4. Provide three smaller textboxes for the digits of the Social Security Number. Set up your program so that after the user types three digits into the first box, the cursor moves to the second box; after the user types two digits into the second box, the cursor moves to the third box. This will require action listeners. Try to put these three boxes on one line.
    5. Make sure that the Social Security number has correct format: three digits, then two digits, then four digits. Optional, no extra credit: after the user types three digits into the first box, move the cursor to the second box; after the user types two digits into the second box, move the cursor to the third box. There is no place to move the cursor after the third textfield, so leave it there.
    6. Remove the requirement (in HW1) that the user press Enter after every textfield entry, which is totally unrealistic. If the user does press Enter, move the cursor to the next field (except for the last group of the Social Security Number). If the user presses Tab, move to the next field. Data will be read only when the user clicks on Submit.
  2. Add a Submit button. When pressed, your program reads the data from the ten text fields, doing the required checking on each. Errors still go to the console. When all are correct, print “Thank you.” NOTE: this does mean that the user may hit Enter after each field or not. Your program should react correctly either way. And, either way, don’t report errors until the user clicks on Submit.
  3. If there are errors, provide helpful but brief notifications. Do NOT remove the erroneous entry, but change the text entry to red. When correct data is entered, change back to black.
  1. All fields need action listeners, except for the last Social Security Number box. This is going to get repetitive. Perhaps we’ll figure out some way (later in the semester) to build a “factory” that writes such code; this is such a common pattern that it is recognized as a
    standard Design Pattern.  Process everything in response to a click of the Submit button. Then print the information in the appropriate choice shown in these examples.

 

Daniel D. McCracken

123-45-6789

160 Cabrini Blvd. Apt. 136

North Salt Lake City, Utah 84054

212-568-6432

 or

 Daniel McCracken

123-45-6789

160 Cabrini Blvd. Apt. 136

Saint Louis Obispo, CA 93401

2125685381

 

Note that you supply the period after the middle initial, and that if the user does not enter a middle initial there should not be any blanks where that would have been. Note also that you do no error checking on the city and state. (“Saint Louis” in the sample output should be “San Luis.”)

The presence of the middle initial is still optional. If present, print it as a capital letter, regardless of how the user entered it. The method toUpperCase() can be applied to a string.

The checking of the ZIP code, the telephone number, the city, and the state should all be done using regular expressions and the Java match method. Regular expressions are very powerful; everybody using a computer at your level has to know them. See page pp 1378ff in the sixth edition and pp 543ff in the fifth edition. Or Google for a tutorial. (Checking the street address would be horribly complicated; don’t touch that. There are a gadzillion legitimate variations.) Make sure that all characters in the Social Security Number are digits.

Feel very free to use Refactor/Rename to improve the understandability of variable names.

But please do not get excited about the exact format of the output. If this were a course in website usability, we’d worry about a lot of things. But we’d also have the knowledge of how to use layout managers, etc. (Which we will do later.) Let’s learn what we can by finishing this assignment, and move on to other things. There is so much interesting and useful stuff to choose from, and only 14 weeks in the semester!

Back to the top of the Spring 2007 CSc 221 page

Back to Dan McCracken's Home Page