CSc 221 Spring 2007 Homework 1

Homework is always due at the beginning of class on the Tuesday, February 13.. (If I don't say "at the beginning of class," 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. Also: I sometimes give out a model solution on the due date, followed by a quiz on what you should have learned doing the homework. There are no make-up quizzes, but absence for valid reasons will be accepted.

Yes, there will be a quiz that day.  Oops. I didn't announce this in time. Make that Tuesday, Feb 20.

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 HW1StarterKit.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 anything other than five digits.
  3. Reject a middle initial (which is still optional) if it is not one character in length; reject it if it is not a letter.
  4. For all four fields: if the user places spaces before the entry, the trim() method can be used to remove the spaces in what is stored as the value. But the textfield that the user sees will still show the spaces. Use the setText() method to place the value—without the spaces—back in the textfield. (An embedded space, as in “Smith Jones,” is OK. The trim() method won’t touch it.) Check out the lastNameEntry code in HW1StarterKit.java, where this operation is already done for you.
  5. For all fields except the ZIP code, when the user presses Enter, the cursor should move to the start of the next field. Here’s a big hint: else middleInitialEntry.requestFocus();
  6. 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:

    10033: Daniel D. McCracken
    10033: Daniel McCracken


    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 expected results.

We will make this application much more realistic in later homeworks, starting with HW2. For a first homework you have a lot of things to learn: how to use Eclipse, 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 a modified version of the starter kit program on the course CD, after the user entered some data:

Here is the Console output (an Eclipse region) when the user pressed Enter after each entry:

Forcing the user to press Enter after each data field entry is highly unrealistic. We'll fix in the second homework. And if things don't look right as you work on the solution, maybe you forgot to press Enter after each item. (How do I know this? Think about it.)

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 two weeks or so. There are a lot of concepts and details here.

Back to the top of the Spring 2007 CSc 221 page

Back to Dan McCracken's Home Page

 

PS: If  you look the HTML for this page (right-click anywhere and pull down to View Source or similar), you will see some truly ugly stuff. What you get when you copy and  paste from a Word document using FrontPage. Unthinkable on a production Web page. Saves a LOT of time for me, on something like this, so I hold my nose and do it. Don't try this in 473!