CS 4773 Object Oriented Programming, Spring 2000
Assignment 3

Due Monday, February 28


This assignment expands upon the applets from the Threads section of the notes. You may obtain copies of the programs from the section in /usr/local/courses/cs4773/spring2000/examples/set4.

The PingPongAppletCanvas implements DisplayInfo, meaning it has a showString method. The PingPongThread takes a parameter which is an object that implements DisplayInfo. Suppose you want PingPongThread to be able to call the showString method of any number (0 or more) objects that implement DisplayInfo. In this assignment we will examine how to do this.

This will work like the ActionListener interface. A button allows you to add any number of ActionListeners and calls the actionPerformed method of each of them added with addActionListener.

In this assignment you will write a new thread, PingPongActionThread, which is like PingPongThread, but has an addStringShower method:
   void addStringShower(StringShower ss)
where StringShower is an interface with one method, showString.

Do an incremental design of this. You will be guided through the steps. Each time you do a new step, start in a new directory. At each step, copy all of the files to a new directory and make the modifications there. Always keep the working copies from the previous steps.

Step 0. Get copies of DisplayInfo.java, PingPongThread.java, PingPongAppletCanvas.java and the two audio files ping.au and pong.au. Create a makefile to compile and run these and make sure they work correctly. Modify the applet so that it displays a label below the Start button which contains> the step number and the programmer name (yours).

Step 1. Change the name of the DisplayInfo interface to StringShower and modify it so that it will work with this name. Change the name of PingPongThread.java to PingPongActionThread.java and the name of PingPongAppletCanvas.java to PingPongActionApplet.java and modify them so they will work with these names and with StringShower. Modify your makefile accordingly.

Step 2. Modify these so that instead of passing the interface in the constructor of PingPongActionThread, it is added with an addStringShower method. Now allow more than one StringShower to be added. Your design should allow an arbitrary number of these. Possible methods of implementation include an array, a linked list, or a Vector. Make sure it does not crash (null pointer exception) when no StringShowers have been added.

Step 3. Replace the three TextAreas with a public class which extends TextArea (call it TextAreaStringShower) which implements StringShower. The showString should append a line to the display. You must insert the newline. Add these as listeners to the appropriate ping and pong threads. Never call the append method of these classes. This should have the same functionality as before, but without the thread count being displayed on each iteration.

Step 4. Make the canvas an external class. Call this class StringCanvas. Put the Image buffer in this class and have it take care of resizing the image. The applet will no longer contain an Image or Graphics. It will have a public method called:
   void drawStrings(String[] strs)
which draws the array of strings, one per line in the canvas. The applet will call this method to draw the same three lines in the canvas as before. Make the background of the canvas orange and the foreground black. Make sure the canvas handles resizing and override update so that there is no flickering.

Step 5. Have the StringCanvas implement StringShower. Use this to feed input into the StringCanvas. Remove the method drawStrings. The canvas will keep track of the number of times the ping and pong threads have called showString.

Step 6. Have the Applet contain just the start button. The applet should pop up a frame containing the canvas and the three StringTextAreas. The title of the Canvas should be what was formally in the label which contained your name and step number. The applet should control the threads, and the frame should display the output.

When you have finished, create a link on your course web page to a page which contains links for running all of parts of this assignment. Insert commentary on that page indicating which parts are running successfully. Hand in source code for the highest numbered step that you implemented well. Create CRC cards for the highest numbered step you created. Put a link to these on your web page and hand in a hard copy.

Step 7 (optional) Add whatever features you like.