[an error occurred while processing this directive]

Programming Assignment 2 - Step 1


Setting Up

For testing the Queue class that you will be writing, you will need two .java files that I am providing:

If you are logged in directly to a UNIX account, you should be able to download these files by holding down the "Shift" key and clicking on the links. Popup dialogs from your browser will let you chose where the files are placed. If you are logged into a UNIX account indirectly (using telnet or XWin-32), this will not work because the files will get copied to the machine that you are working on rather than your UNIX account. In that case, you should be able to get a copy by dragging and dropping the code into an editor running from your UNIX account.

In addition to the Getter.java and QueueTest.java files, you will need access to the class libraries that I have set up for the course. If you are doing your work on a UMD UNIX machine, the CLASSPATH setup in the first lab should take care of this. However, if you downloaded my libraries for work on your own computer then you will need to download them again. I have added some classes that are used in this lab.

Writing a Queue Class

You should implement a first-in-first-out queue using a linked list. The best way to implement linked structures in Java is to use a nested class for nodes. For your Queue class, your Queue.java file starts off like the following code.
public class Queue {

    static class Node {
	Object item;
	Node next;
    }  // static class Node

    private Node front;
    private Node rear;

}  // public class Queue

With this code, you are defining two classes: a Queue class and a Node class. The full name of the Node class is Queue.Node, but you can just refer to as Node in the Queue class code.

Declaring a nested class like Node makes all of its members accesible in the outer class. Thus references such as front.next are legal in the Queue class code.

Note that front.next is a reference rather than a value copy. The dot in Java means the same thing as "->" in C++ code. This may take some getting used to, but in the end, I think you will find it more natural than C++. Yes, you do have pointers in Java, but they are hidden in the language implementation where they cause less confusion.

With the above skeleton code, you are ready to begin implementation of the Queue class. When your Queue class is fully implemented, it should have the following constructor and methods:

Constructor

Methods

Note that get() should not remove the object at the front of the queue.

Before implementing the Queue constructor and methods, you may want to add constructors to the Node class with parameters for initializing its object and next members. This is just a convenience to simplify code for the Queue add() method.

I leave it to you to provide the implementation of the Queue constructor and methods. Take care with both add() and remove(). You will need to deal with two cases in each of these methods.

Testing Your Queue Class

To test your Queue class, you just need to compile all of the .java files with the following command.

javac *.java
Then you can run the test program with the following command.
java QueueTest

Interpreting Test Output

Although you do not need to understand how the Getter and QueueTest classes work, you do need to understand what kind of output you are getting. Both of these classes access classes in my gshute.sync package. This classes in this package will be explained in detail at a later time. For now, you just need to understand that the classes support simulation of multiple processes accessing shared data. One of the classes can be used for creating a log of important events. The output that you see when you run QueueTest is one of these logs. It contains entries like the following.

0751: Getter 1 gets 15.
The number at the beginning of the line is a millisecond timestamp. The rest of the line is a description of what happened at that time. All such descriptions begin with the name of a process ("Getter 1" in this case) that made it happen.

Before any log reports, the QueueTest main program sets up a Queue using your implementation and adds Integer objects with values from 0 to 99. The Getter 1 process gets and removes the objects until the simulation is over (about 1000 milliseconds). Due to some randomized time delays programmed into the Getter class, you will see some variation in the number of gets performed by Getter 1. The average is about 20. They should be in ascending (first-in-first-out) order. After each get, you should see a report like the following.

0763: Getter 1 removes an item.
If your output does have ascending "gets" values and "removes" alternating with "gets", then your Queue is working correctly. Then you are ready to move on to Step 2. There you will monkey up the works by running a second process named "Getter 2". [an error occurred while processing this directive]