An Introduction to Java

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.


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:

To compile and run the program, you'll need to work in a shell (terminal) window. First you'll need to change directory into the directory where you have the files, e.g.

   cd java/CompuDuds
Now you can compile each of the files; for example, to compile, you'd use the command
You need to do the corresponding command for each of the other four files as well. The most important thing to remember is this: if you make any change to any of the files, you must recompile that particular file before you can test the program, and you must save it out of the editor before you recompile it. In other words, after making any change, you have to save, then compile, then test. (You will be using an editor of your choice to edit the java files; we suggest emacs. Be sure to ask for help with emacs if you need it.) When you add a new class, it should go in a file of its own, named after the class. You should then compile that new file as well.

Once you have the five original files compiled, 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, use the command

   java CompuDuds

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.

Java puzzle

The directory puzzle contains the java applet described in section 15.3 of the text. Assuming your shell still has the CompuDuds directory as the current directory, you can switch to the puzzle directory using
   cd ../puzzle
because .. is a name for the "parent" of the current directory. That is, if you are down in the CompuDuds directory, .. is the java directory and ../puzzle is its puzzle subdirectory.

To run the code, first compile all of the Java files with

   javac *.java
and then run the appletviewer on the HTML file:
   appletviewer puzzle.html
Once tested, add the ``Randomize'' button described on in Exercise 15.8 on page 615.


Do exercises 15.14 through 15.17. The exercises begin on page 637, and the code is in the directory interest.


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


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.