Computer Science 60
Principles of Computer Science
Spring 2005


Assignment 4: A Taste of Polymorphism and a Minefield of Spam!
Due Monday, February 14 by 11:59 PM.

Please start this assignment early. It is fun and challenging! Most people find it a step up in challenge level from the previous assignment.

The assignment has two parts. The first part is a short programming exercise which uses inheritance to build polymorphic double-ended queues or "deques" (pronounced "decks"). You'll use your deque class again in Assignment 5's "Spampede" game. The second (larger) part of the assignment is to write a Spamsweeper Applet. You can do these parts of the assignment in any order - they are entirely independent of one another. In particular, however, we will cover the Deque problem in more detail on Thursday, so you may want to start (if you start early!) on Spamsweeper.

Playing with a full deque! (20 Points)

First, we have provided some files for you which you will find in the directory /cs/cs60/assignments/assignment4/Deque. Copy all of these files into your own directory.

One of these files is Queue.java, an implementation of a polymorphic queue using linked lists. Notice that the polymorphism simply allows the queue to contain any Object. The Node class used by this queue is at the bottom of the Queue.java file. (Java permits multiple classes to be defined in the same file.) You'll notice that this Queue class has some of it's methods renamed from the usual names. Specifically, dequeue has been renamed dequeueFront and enqueue has been renamed enqueueBack. There is also a method called peekFront which returns the element at the front of the queue without removing it.

Your mission is to implement polymorphic double-ended queues, or "deques". A deque is like a queue except that data can be inserted (enqueued) at the front or at the end and, similarly, data can be deleted (dequeued) from the front or end. Your deque will be polymorphic: It will be able to deal with any types of objects. Since a deque is an abstract data type, it must implement a provided DequeADT interface which you will find in the file /cs/cs60/assignments/assignment4/Deque/DequeADT.java which looks like this:

// DequeADT.java

// A set of methods that a Deque must implement to
// ensure that it has all the expected capabilities
// and a few special capabilities as well!

interface DequeADT
{
  public Object dequeueFront();        // Also in Queue
  public Object dequeueBack();         // New!  Remove element at back

  public void enqueueFront(Object o);  // New!  Add element to front
  public void enqueueBack(Object o);   // Also in Queue

  public Object peekFront();           // Also in Queue
  public Object peekBack();            // New!  Return element at back

  public Object peek2Front();          // New!  Return 2nd to front element
  public Object peek2Back();           // New!  Return 2nd to back element

  public boolean isEmpty();            // Also in Queue
  public String toString();            // A good thing to have

  public void reverse();               // New!  Reverse the order of elements
}

Here are the details of the Deque methods you need to implement this week:

Inheritance:   Even though there are eight methods in the above DequeADT interface, you will not have to implement all of them! The reason for this is that you are provided a Queue class (in Queue.java). Since a deque is a kind of queue, you will use inheritance so that your Deque class extends the capabilities of the existing Queue class.

What about Node?   Inside the provided Queue.java file, there is a Node class that implements a singly-linked list node. Because your Deque class will require doubly-linked list nodes, you will need to create a DNode class, which extends and inherits from the Node class. The start of both the Deque and DNode classes are available in the file Deque.java.

Some Important Notes:

Submission   The easiest way to submit is to send us all of your .java files with the command

  >  cs60submitall
This will automatically submit all of the files in the directory in which you currently reside.



Spamsweeper! (80 Points)

Now for Spamsweeper! Note that the applet that you will be writing will run under most (but not all) web browsers. In particular, we have found that some versions of Netscape have trouble running some applets.

Another reliable and very convenient way to run an applet is using appletviewer on turing. To do so, you must be using a terminal in the CS terminal room. (You can also use an Xwindows client on your own computer - but if this isn't something you're familiar with, you're best off in the CS terminal room.) Again, most browsers (Internet Explorer, newer versions of Netscape, Safari, Mozilla, and others) will also run this applet.

First, click here to see what the Spamsweeper applet will look like when you're done. You will need to click on the "reload" (or "refresh") button on your browser each time you want to start a new game after the first game. (Depending on where you are running your browser, you may hear some sound effects. The sound effects are NOT required! You may add sound effects if you wish, but it's not required. We'll talk about sound effects next week, but in the meantime you may snoop around on the web or in a Java book to learn more about it.)

Your program should have the following features:

We've left the basic components of Spamsweeper, as described in class, in the directory /cs/cs60/assignments/assignment4/Spamsweeper/. We've also left you the MicroMouse.java and MicroMouse.html files demonstrated in class, so that you can look these over, modify them, etc. to get more familiar with how applets work.

Copy all of these files into your own directory. Also, copy the file /cs/cs60/assignments/assignment4/copyFiles into your directory. (More on this later.)

You will be writing all of the interesting code for this program! In particular, you will need to write the constructor for Gameboard.java and all of the interesting methods in Spamsweeper.java. You should compile Spamsweeper.java in the normal way.

To run the applet, you can simply type appletviewer Spamsweeper.html. Remember, the appletviewer will only work if you are on a terminal in the CS terminal room (or have X-windows installed on your own machine).

Another alternative is to run the applet from your web site. You can do this from almost any browser as follows:

Your best bet is to use the provided ./copyFiles script to copy your files as described above. However, if you really want to do the copying yourself, you must be careful to only copy html and compiled .class files to your web space, because anything there is potentially globally viewable. Having your source code globally viewable is against the department's code-sharing policy and the HMC honor code. If you use the provided ./copyFiles script, all of this is done for you automatically and there's nothing to worry about.

Debugging:   We strongly recommend that you write your program incrementally, and test it for correctness as you go. From our experience, this can reduce the debugging time very substantially. For example, after writing the constructor for the Gameboard class, write a simple tester program to make sure that it's working. Similarly, make sure that your applet can draw the grid before worrying about filling it in.

What happens to my System.out.println commands? Printing out "stuff" is a key debugging tool. However, System.out.println commands will not be displayed in the same window in which your applet is running. If you are using the appletviewer, your System.out.println commands will simply cause text to be printed in the window from which you invoked the appletviewer. However, if you are running your applet from a web browser, you'll need to take one small extra step inorder to see your System.out.printlns: You will need to "ask" your web browser to display these print statements in something called the "Java console". This is a little window which appears next to your browser window and contains the printed out text from the System.out.println commands. Here's how to do this:

Please sumbit ALL of the files associated with your solution, including all files ending with java and html. Your best bet is to use cs60submitall so that you don't overlook submitting any file. That command will make sure to submit everything you need!

For Totally Optional Bonus Credit create an implementation called Bonussweeper.java applet (and any other auxilliary files) which augments Spamsweeper with one or more of the following features (up to 3 bonus points for each feature, up to a maximum of 9 bonus points):

Make sure that at the top of the file Bonussweeper.java you clearly document which of these features you have added.

Last modified February 2005 by Ran Libeskind-Hadas or Zach Dodds