Homework 1 (both sections).

Due dates: 221 G: September 14, 2005

                  221 P: September 15, 2005


Homework is always due at the beginning of class on the date given. (If I don't say that, people will be rushing in at the end of the period, out of breath, having missed the entire lecture. This is not good education.) Automatic 2 point (of 10) penalty for lateness.

This homework will be done in teams of two, which I will assign. I strongly urge you to work together closely. One proven technique: the two of you sit at one computer. One person types, the other watches, corrects, suggests, etc. Every 20 minutes or so you trade places. This is called pair programming, and is widely used in industry as well as in education. Try it. You might be surprised how effective it is.

Of course you can still work separately, on your own computers at home or wherever, and exchange code via email. But even if you do that, give pair programming a try anyway. And whatever else: each assigned team turns in one program.

 

Modify HW1StarterV3.java, on the course CD, to do the following things:

  1. Add a label for a (required) ZIP code.
  2. Add a textfield for the ZIP code. Reject the input if it is not five characters in length; reject it if there are anything but digits.
  3. Reject a middle initial (which is still optional) if it is not one character in length.
  4. For all four fields: if the user places spaces before the entry, the trim() method can be used to remove them in what is stored as the value. But the textfield will still show the space. Use the setText() method to place value—without the spaces—back in the textfield. (An embedded space, as in “Smith Jones,” is OK. The trim() method won’t touch it.)
  5. When user presses Enter for the ZIP code—assuming the entry is valid—print the user’s name and ZIP, following the pattern of these examples:
    Daniel D. McCracken 10033
    Daniel McCracken 10033


    Note that you supply the period, and that if the user does not enter a middle initial there should not be any blanks where that would have been.

 

All output still goes to the console, which is highly unrealistic. We will fix that in later homework.

Recall that the user must press Enter after entering data into any textfield, to activate the action listener on that field. There is no way to enter all the data, without pressing Enter, then click a Submit button. Also highly unrealistic; will also be fixed in future homework. It is still permissible to enter the data in any order, but in this version of the HW the ZIP needs to be entered last to get reasonable results.

We will make this application much more realistic in later homeworks. For a first homework you have a lot of things to learn: how to compile, how the starter program works, Java syntax, and more. We don’t try to do everything in the first homework.

Here is the GUI produced by the starter kit program on the course CD, after the user entered some data:

 

Here is the console input when the user pressed Enter after each entry:

 

It is very important that you do your own work on this. (In teams, of course.) You will be expected to be able to explain it, on a quiz. In a few weeks. There are a lot of concepts and details here.

 

 Back to Dan McCracken's  Home Page