Harvey Mudd College
Computer Science 60
Assignment 6, Due Thursday, March 2, by midnight

Spampede!

Assignment 6 takes advantage of the double-ended Queue (Deque) class you wrote in assignment 5 in order to implement a centipede in search of spam within a simple Maze-like world. This will give you the chance to use Java's graphics library and event-handling code. The Java 1.1 event-handling mechanism will be used.

Example Applet

You can run an example Spampede applet at www.cs.hmc.edu/~dodds/Applets/Spampede/Spampede.html .

Files and Classes to Create

You will need to create a file Spampede.java that contains at least the following classes:



Note that only the Spampede class is entirely new for this assignment. An example applet's source code is available in the file /cs/cs60/as/a6/ExampleApplet.java. Its applet can be seen at www.cs.hmc.edu/~dodds/Applets/ExampleApplet/ExampleApplet.html .

A copy of ExampleApplet.java is named Spampede.java in the /cs/cs60/as/a6 directory. To get it, the HTML file you will need, and a script for compiling and copying your Spampede applet, you can do the following:

cp /cs/cs60/as/a6/*Spam* ~/cs60/a06

OPnce you have done that, you should be able to compile Spampede and copy it to your public_html directory with the single command

./makeSpampede
from your ~/cs60/a06 directory.

What does the code have to do?

The Spampede applet gives a user control over a spam-seeking centipede. Key presses from the keyboard change the direction of the centipede's movement in order to intersect snacks that appear at random places on the screen. For each snack consumed, the centipede grows by one segment (a segment is simply one MazeCell).

The particular applet behaviors and methods you need to implement are



Use double-buffering, as discussed in class and shown in the ExampleApplet applet, so that your applet does not flicker. This is built in to the starting file provided.

I want more!

If you haven't had enough of the Spampede.java file at the end of this assignment, there are a couple of specific items and an open-ended invitation to improve on the applet for up to 20% (or beyond...) optional bonus credit.



In addition, the extra credit for last week's assignment is extended through this week. If you would like to create an applet representation of the SpamSeeker (as indicated in that assignment), you may submit that in a separate file for additional credit.

Testing your applet

You can test your code in two ways:

  1. With the appletviewer: if you are working in the terminal room or have an X connection to a remote machine, you can test your applet with the command
    appletviewer Spampede.html
    
    from any directory which contains both the compiled java code (Spampede.class, etc.) and the Spampede.html files, available for copying from /cs/cs60/as/a6.

    Caution! For me, turing's appletviewer takes a long time to appear and run (> 1 minute) unless the line

    setenv JAVA_FONTS "/usr/local/java/jre/lib/fonts"
    
    is added to my .cshrc file (which is then "sourced") before running. This line speeds the search for fonts, but error messages still appear that some fonts are not found.

    Appletviewer seems to process events very slowly in any case.

  2. With a web browser: (preferred!)

    Documents you place in your public_html directory (which should be in your home directory) will be publicly available on the web. If you copy the files /cs/cs60/as/a6/ExampleApplet.html and the .class files (obtained by compiling) to your public_html directory, you will be able to view the ExampleApplet applet at URL http://www.cs.hmc.edu/~username/ExampleApplet.html, where username is replaced by your turing login name.

    A script for compiling and copying all the necessary files is available at /cs/cs60/as/a6/makeSpampede. You can copy that file into your directory and then run it as

    ./makeSpampede
    
    (You need the extra ./ if . (dot) is not in your path.

    Note Netscape will reload newly-compiled applet code if you hold down the "Shift" key while pressing the reload button. Internet Explorer will reload applet code if your hold down the "Ctrl" key while pressing the refresh button. (I suppose they're different to avoid lawsuits ... ?).

    Note #2 Netscape has a "Java Console" available from the Communicator -- Tools menu. You should have this open as you test your applet. With the Java Console, you can include System.out.println messages anywhere in your code and see what gets printed. In addition, runtime errors will get reported there. This is a good thing! Debugging without it is bad! (Spoken from first-hand experience.) I don't know how to use the Java Console on IE (if anyone does, please let me know.)

Reading

This assignment takes advantage of Object-Oriented programming (covered in Chapter 7 of the book), but the details of Java's Applet and GUI Component classes are available from the references page. Also, the basics of event handling and Java's built-in classes will be presented in class.

Submission

You should submit your Spampede.java source file in the usual way, i.e., by running

% cs60submit Spampede.java
You will be asked to input the assignment number (6).

You should not submit your Spampede.html file.