An Introduction to Java

MCS-178: Introduction to Computer Science II
Max Hailperin, Spring 2002

Due: May 22nd, at the beginning of class

Getting started

In this lab, you'll have three goals To make a copy of all the files, the simplest approach would be to use the shell to copy the whole directory containing the files. In a shell window, type
   cp -r ~mc28/labs/java/java .
(Note that this command ends with a space and then a period. Ask for help with this if you need it.) You will get a new directory called java. This directory contains three subdirectories, one for each portion of the lab. All the individual java files you will use are in one of those subdirectories.

Using BlueJ

You will use a program called BlueJ to edit, compile, and run the three Java programs. Karl will demonstrate BlueJ in class; you should also ask for help in lab if the mechanics are confusing you.

To run BlueJ, in a shell window type

   ~mc28/labs/java/bluej/bluej &
There will be all sorts of error messages in the shell window about fonts not being found; these are a consequence of how our Java software was installed and don't cause any particular problems other than looking alarming.

Each of the three portions of the lab corresponds to a separate "project" in BlueJ. You will open each of them from within BlueJ, using the Project menu's "Open Project..." option. They are all found within the java directory you copied. More specifics about each project are in the following sections of this lab handout.

When you open a project, BlueJ will show a UML diagram of the classes. The style of diagram it uses is very cluttered with dotted arrows between classes; these dotted arrows indicate that one class makes some use of the other class. To de-clutter the diagram, you can turn these arrows off, by clicking the checkbox labeled "Uses".

To perform any operation on any class or object shown in BlueJ, you can use the right mouse button to click on it. This will bring up a menu of the different operations you can perform. One operation on classes is so common that there is also a shortcut: to open a class in the editor, you can just use the left mouse button to double click on the class.

BlueJ's text editor has only limited support for indentation. Unlike DrScheme, it will not automatically indent each line the correct amount. Instead it will indent each new line the same amount as the line above it. You can indent in further using the tab key, or back out with the backspace. You can also select a block of lines with the mouse and then move that whole block left or right using the F5 or F6 key.

Note that printing from BlueJ doesn't seem to be working at the moment. To work around this, you can print Java files using shell commands. For example, you could do

  mpage -2f java/CompuDuds/ItemList.java | lpr -Pmcs-lab

CompuDuds

First, you'll make a few minor alterations to a Java version of the CompuDuds program from our textbook. The point is to become familiar with the mechanics of Java. The program is divided into five classes, each in its own file. The five files are:

The first thing to do is to test the program out without yet having made any changes. That way you have a baseline for comparison: if it isn't working, you aren't the one who broke it. To run the program, right-click on the CompuDuds class and select the main method. In addition to running the program in this way, you can try making and interacting with individual objects directly through BlueJ, as demonstrated in class. This will be valuable in the testing and debugging you do later in this part of the lab.

Now you can start making the necessary changes, and test and debug as you go along. Perform exercises 15.2 through 15.4 on pages 598-599 of the text. You will probably find yourself wanting to make a new class that is very similar to one of the existing classes. In order to do this the easy way, use BlueJ's button to make a New Class, which will be initially empty. Then open both that new class and one of the old classes up in the editor, and use the mouse to select the contents of the old class, copy them, and paste them into the new class. Now edit the new class to differ from the old one as appropriate.

Java puzzle

The project named "puzzle" contains the java applet described in section 15.3 of the text. One you have opened the project in BlueJ, to run this applet, you right click on the Puzzle class and then select "Run Applet". Once you have tested running the applet, add the ``Randomize'' button described on in Exercise 15.8 on page 615. As with the CompuDuds project, you may want to copy the contents of one of the existing classes as a starting point for a new class.

Concurrency

Open the project named "interest" and do exercises 15.14 through 15.17. The exercises begin on page 637.

Optional

If you have the time to get some more experience with Java, do exercise 15.18 on page 639.

PostLab

The lab writeup can be short and to the point. Highlight the changes you had to make, and the testing you did. For the concurrency section, you will need to provide a few paragraphs of text to answer the questions about invariant preservation and race conditions. You do not need to write up an introduction or conclusion. For this lab only, you may assume the reader has read the textbook.