Java and Concurrency

MCS-178: Introduction to Computer Science II

Getting started

In this lab, you'll have four 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. David 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

   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. In fact, you may want to miniaturize the shell window, since the error messages can get rather annoying.

Three of the four portions of the lab correspond to a separate "project" in BlueJ, and are contained in separate subdirectories. (You will create the fourth project, corresponding to the new java puzzle, ourself.) 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 its 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 unclutter 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/ | lpr -Pmcslab


First, you'll make some 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 as well as exercise 15.20 on pages 640-641 of the text. For exercise 15.3, you will need 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.

For exercises 15.2, 15.4, and 15.20, you should simply modify the class ItemList. In particular, if you should not create a new class called ThriftyItemList like you did in homework exercise 14.18. Furthermore, when submitting this part of the project, just submit the final form of the CompuDuds directory, which contains all the changes for the four exercises together.

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.

New Java puzzle

Create a new java puzzle by doing exercise 15.9 on page 616. You should be sure to create a whole new copy of the puzzle directory so that you do not alter the already-existing 15-puzzle. You should also eliminate methods that were needed in the 15-puzzle but which are irrelevant to this new puzzle.


Open the project named "interest" and do exercises 15.14 through 15.17. The exercises begin on page 637. Leave both the sleep commands and the synchronize keywords in your code. (The synchronize should get rid of the problems exposed by the calls to sleep.


If you have the time to get some more experience with Java, ask one of us about additional programs you might write.


For this lab, you should:
  1. submit the code that you wrote (see the section "Submitting your code" below for instructions on doing that)

  2. for the concurrency section, you should hand in a short paper providing a few paragraphs of text to answer the questions about invariant preservation and race conditions.
Otherwise, you do not have to hand in a written report.

The gradesheet for the project is available in PostScript or PDF format. (If you print a copy out, you can staple it to the front of your project report to save paper.)

Submitting your code

To submit your code, do the following:
  1. Make sure your code is documented. In particular, report any bugs in a comment at the head of your file. You should also describe in the header the testing you have done. In particular for the CompuDuds portion, you should write a main() method in ItemList which does the testing. Make sure that you have generated the right documentation files and that they look good. Note that you can generate the documentation files directly from bluej.

  2. Be sure that all of your code is properly indented. You can do this in emacs if you wish.

  3. In a shell, cd to the directory containing the java directory whcih contains the four portions (subdirecotories) of the lab, and type ls to make sure you are in the right place. Finally, type:
      ~wolfe/public/178/submit java