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.

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 CompuDuds class by typing:

   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. Use the cp command to copy the existing .java file to a new file, and use emacs to edit the new file as appropriate.

For exercises 15.2, 15.4, and 15.20, you should simply modify the class ItemList. 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. You should also eliminate methods that were needed in the 15-puzzle but which are irrelevant to this new puzzle.

Concurrency

In a shell, cd to the directory 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.)

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 should write a main method in ItemList which does the testing.

  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 that 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