CSc 221 Spring 2007 Script for Second Lab
In the lab--or at home--here is a roadmap for starting HW1. Understand: we are mostly trying, right now, to learn Eclipse.
We'll pick up a few ideas about Java, too, but that's a secondary focus
for today.
Whenever you are ready to leave the
lab, close Eclipse and send yourself and your partner email with your
Java file attached. Or copy to a USB drive, or burn a CD. But email is
simplest--unless you have spam-blocking issues. USB drives are cheap and
convenient.
- Start Eclipse. In the lab, the workspace has to be a file in
C:\temp.
The name doesn't much matter, except make it meaningful--to you.
- Create a
New/Project. Name it
Homework1.
Accept all defaults on the first dialog. Click
Next.
You will get another dialog; accept all defaults and click
Next.
- Right click on the project name and create a new package. Name it
hw1. The fact that we have a package named
hw1 in a different project causes Eclipse no confusion.
But if the duplication causes YOU confusion, then use something else.
- Right click on the package name,
Import/File system and navigate
to wherever you can get
HW1StarterKit.java..
That will be the name of a folder that contains the file, remember, not the file name
itself. (You could get
it from the student CD.) Click OK.
- You will be given a list of all the files in that
folder. Select
HW1StarterKit.java. and click
Finish.
Ignore
.class
files, if present.
-
The file will show up in your Package Explorer View.
Double click to open it. Ignore warning messages, unless you used a package
name other than hw1. In that case Eclipse will complain that the declared
package doesn't match the expected package. It will complain by putting a
red mark in the left margin. Let the pointer hover over that for a few
seconds and you will see the error message. Or click on the Problems tab
below the editor panel. Many errors, including this one, can be fixed by
holding down the right button over the error mark and pulling down to Quick
Fix. In this case you will be offered two choices. Pick: Change package
declaration.
- Select
HW1StarterKit
in the class definition. Right click Refactor/Rename
and rename to
HW1.
Click Finish. This
also updates all references to that name (there are two in the program), and the file name.
Note the
change in the Package Explorer View. Ignore a warning about a main method.
Refactoring is a big deal; this is the tiniest hint of its power. (The last
time I did this is got an error: Invalid Text Selection. I have no idea what
that is about. Workaround: select the class name, right click and pull down
to Refactor/Rename. Sigh. Aren't computers wonderful?) I also got a warning
message about the
main
method. Ignore it. Some information windows may be shown; dismiss them after
studying.
- Save and
Run/Run as/Java Application – to make sure you
are starting with something that works. That is, type in some data, press
Enter, and make sure it prints in the Console window. Can be very quick:
there is no error-checking in the starter kit, so you can type anything at
all.
- In the comments at the beginning of the program, change date and name(s).
YOU MUST put the names of team members in ALL program files that
you submit in this course. After a grace period, failure to do so
means an automatic loss of points on homework. I'll talk about this is
class. It's a convenience for me, and the barest suggestion of the
documentation that is a crucial part of software development.
-
We are now about to make some of the modifications required for homework 1.
If you wish you can see the complete assignment, at
CSc 221 Spring 2007 HW1. But
you don't need that document open to do this homework.
- Add declarations for the new ZIP code label and text
field. Copy the code at the bottom of this page that is identified as
"Snippet 1" to a position after the last name entries. Indentation will
probably be wrong; easy to fix by selecting the code in question and
pressing
Shift/Control/F. (F for format.) A free service of Eclipse. You
can Select
All
and do the same thing for the whole program. That will format the code
according to Sun recommendations, which will place a lot of blank lines in
the declarations. We are rather fussy about formatting, but not so much
about that particular detail. Your choice.
- Copy Snippet 2 to just before the end of the class,
which is the closing right curly brace.
- OK. I've done enough for you . Now you do some work.
Copy the entire method that begins:
private
JTextField getLastNameEntry() {
to a position (again) just before the closing final brace.
- Error message on first line: duplicate method. How
true. Fix it, with the editor.
Refactor/Rename
is no help here, because we don't want to change all instances of the
name. (In case you're lost, you want to change the first line to read:
private
JTextField getzipCodeEntry() { )
- All the field names are wrong, too. Change last name
stuff to ZIP code stuff, using the field names in Snippet 1.
Refactor/Rename
won't work here, either. If you're adept with cut-and-paste, the changes
won't take ten minutes. Maybe less.
- Copy the two lines of Snippet 3 to the end of the
method private
JPanel getJContentPane() {
- Save (Cntl/S),
which compiles, remember. If there are Problems (also red marks in left
margin) fix them. Hard for me to guess what your errors might be. I'll help
in lab, or your classmates can. Amazing what another person can see, that
you can't. Happens to everybody, myself most definitely included.
- Once you have a clean compile, run it! See if you got
this much right before going on. Save and run:
Run/Run As/Java
Application.
- Oops! The window isn't high enough. Change the
dimensions in the
initialize()
method to
this.setSize(350, 200);
- Run again. Since you are running the same
program you ran before, you can do
Cntl/F11,
or click on the button in toolbar with a white triangle in a green circle:

- Now we need to check that the ZIP is valid, i.e.,
exactly five digits. I'm going to give you this one; it uses a regular
expression, which is HW2 stuff. Copy Snippet 4 after the code to print the
ZIP.
- (Pause for reflection. This incremental development is
standard industrial practice. Take something that works and modify it, piece by piece,
checking at each reasonable stage to see if what you have done so far is
right. Vastly better than doing all the modifications before trying the
first time. If you do that, you may very well get no output at all--because of
some small mistake--and have no clue where to start looking for the
problem.)
- Add code to the places where the first name, middle
initial, and last name are processed, to delete leading and trailing spaces.
This will be pure cut-and-paste, if you wish, followed by changing field
names. Lots of chance for errors! Work slowly and carefully, and test
each change as you reach a point where it can be tested. Why make the
same mistake three times, when you can catch it the first time?
- Once all that is working correctly, add code to print
the output. This will be an
if
clause inserted after the validity testing of the ZIP. Highly unrealistic,
of course; we'll add a
Submit
button or something on HW2.
- The user may or may not have entered a middle initial,
so you will need another if to handle both cases correctly. The logic here
can get confusing. Fight through it, testing repeatedly, with and without a
middle initial.
- Celebrate or scream, as appropriate. If the result was
"celebrate," you have pretty much finished the homework. Make sure
your names and the date appear as comments at the beginning, and
submit. Email greatly preferred. We'll talk about this.
This is probably at least enough for an hour in the lab. We
don't try to win it all in one day. If you can't finish, save your work and
finish another time, presumably on your own machines.
When you are ready to stop for the day, close Eclipse. If
running on a lab machine, copy your workspace to a floppy, CD, or USB drive. Or,
as before, mail the program to yourself. I’ll show you
how to burn a CD if that isn’t obvious.
Snippet 1: declarations
for the ZIP code label and text field. Copy to a position after the last name
declarations. (Location doesn't matter to the compiler, but it will matter to
you when you are debugging.)
private JLabel
zipCodeLabel;
private JTextField zipCodeEntry;
private String zipCodeAsEntered = new String();
Snippet 2: Insert just
before the closing right brace of the class.
private JLabel
getzipCodeLabel() {
zipCodeLabel = new JLabel();
zipCodeLabel.setText("ZIP Code *");
zipCodeLabel.setPreferredSize(new Dimension(100, 20));
return zipCodeLabel;
} // end private JLabel getzipCodeLabel()
Snippet 3: Add at the
end of the method
private JPanel getJContentPane() {
jContentPane.add(getzipCodeLabel(), null);
jContentPane.add(getzipCodeEntry(), null);
Snippet 4: Add immediately
after code to print ZIP.
if (!zipCodeAsEntered.matches("\\d{5}"))
System.out.println("ZIP must be five digits");
Back to the top of the 221
page
Back to Dan McCracken's Home Page