Harvey Mudd College
Computer Science 60
Assignment 6, Due Friday, October 20, 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 (actually, modify) a Spampede.java file that contains your Spampede applet class. In addition, you will need to use the following classes, which you may spread among several files (such as those in /cs/cs60/as/a6).



To get you started, there are the following files in the /cs/cs60/as/a6 directory:

You should copy all of these files to your ~/cs60/a06 directory with the command
cp /cs/cs60/as/a6/* ~/cs60/a06
(This will also copy a couple of other important files you'll need.) You can see what the skeleton Spampede.java applet does at http://www.cs.hmc.edu/~dodds/Applets/ExampleApplet/Spampede.html. Note that this is just a starting point, and not the whole Spampede applet.

Getting the applet running

So, the first thing to do is to make sure you can get that example applet running on your own webpage. To do this, follow these steps:

  1. You should make sure you have all the files from /cs/cs60/as/a6 in your ~/cs60/a06 directory. If you don't, use the command
           cp /cs/cs60/as/a6/* ~/cs60/a06
           


  2. Copy the Spampede.html file to your "public_html" directory. That directory is inside your top-level home directory. Thus, from your ~/cs60/a06 directory you can type
           cp Spampede.html ~/public_html
           
    You will not have to do this step again.

  3. Make sure that the permissions are set so that people can view your applet. To do this, first go to your home directory with
           cd ~
           
    and run
           chmod 755 public_html
           
    Then, your public_html directory will be readable by anyone.

  4. Next, from your working directory ~/cs60/a06 compile all of the files:
           javac *.java
           


  5. This compilation will produce a large number or .class files. These all need to be copied to your public_html directory with
           cp *.class ~/public_html
           


  6. Finally, you need to make sure that all of the files in your public_html directory are readable by everyone. To do this, go to your public_html directory with
           cd ~/public_html
           
    and then change permissions with
           chmod o+r *
           
    (If you have other files whose permissions you don't want to change, you can replace * with a filename.)

  7. Now your Spampede applet (in it's current state) should appear from any web browser at the address
           http://www.cs.hmc.edu/~<your turing login name>/Spampede.html
           

Writing the applet

Once the above steps work for you, you're ready to write your own applet. Bascially, the starting point is the Spampede.java file. You will want to change that to include an object of type Maze and you will want to write a method that draws a Maze.

As you write your code, please compile and run the applet often to make sure you're on the right path. Bascially, that means repeating steps 4-7 above.

How do I know what going wrong in an applet?

Netscape has a wonderful applet-debuggin feature called the Java Console. To open the Java Console, go to the Communicator Menu, the Tools submenu, and choose "Java Console." A window will open up. If any errors occur during the running of your applet, they will appear in that window.

Also, you can use System.out.println to debug your applet -- all of those print statements will print to the Java Console. This is critical for debugging!

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





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 (but only to 10/13). So, if you would still 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.

Grading/Testing?

You can test your code in two ways:

  1. With a browser. This is the recommended procedure, as described above.

    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!)



  2. 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 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.

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 and other source files in the usual way, i.e., by running

% cs60submit 6
This will gather up all of the .java files and send them to the grading account. No tests will be run; instead the graders will play your Spampede applet to see if it does all of the things it should.