Java and Concurrency

MCS-178: Introduction to Computer Science II

Getting started

In this lab, you'll have four goals: Start by copying the whole directory containing the provided source code 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.

You can complete this lab using either of two programming environments; feel free to ask for help with either:

  1. You can use a text editor (such as emacs) to edit the java files and use a terminal window to issue commands for such tasks as compilation.

  2. You can use the BlueJ programming environment, as introduced in a prior lab. If you take this approach, the first time you open one of the three subdirectories, you'll need to do it with the "Open Non BlueJ..." option in the Project menu.

CompuDuds

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, compile the source files and run the main method in the CompuDuds class. If you are using a terminal window rather than BlueJ, you would change directory to the CompuDuds directory and then type

   javac *.java
   java CompuDuds

Now you can start making the necessary changes, and test and debug as you go along. Do 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. If you are using BlueJ, you can use a menu option to create the new class, then use copy and paste commands to paste the existing class's code into the newly created class, as a starting point. If you are using an editor such as emacs, the simplest option is probably to open the existing file and save it out under the new name. (As usual, I'd be happy to help with the mechanics.)

For exercises 15.2, 15.4, and 15.20, you should just modify the existing classes rather than creating any new class. 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 directory named "puzzle" contains the source code for the java applet described in section 15.3 of the text. Once you have tested running the applet, add the "Randomize" button described 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. If you are using BlueJ, this just entails using the "Save As..." command in the Project menu. For those using commands in a terminal window, if your current directory is set to the java directory (which contains the puzzle directory), the command
cp -r puzzle flip
would make a copy of the puzzle directory called flip.

Concurrency

Using the directory named "interest", do exercises 15.14 through 15.17. The exercises begin on page 637. Leave the sleep commands and the synchronized keywords in your code. (The synchronized should get rid of the problems exposed by the calls to sleep.)

Your solution to 15.14 should be precise. Mathematical assertions, such as invariant (1), are best confirmed with clear notation and equations (and only a little text).

For exercise 15.16, be sure to clearly explain where you put your calls to sleep to make each behavior happen.

Optional

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

PostLab

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 won't be able to thoroughly test all your changes just using the main method that is in the CompuDuds class. The best way around this, especially if you are not using BlueJ, is probably by writing another main method in the ItemList class to do some additional testing.

  2. Be sure that all of your code is properly indented.

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