Harvey Mudd College
Computer Science 60
Assignment 5, Due Monday, February 23, by midnight

Problem 1:     Spampede! (80 points)

Assignment 5 takes advantage of the double-ended Queue (Deque) class you wrote in assignment 4 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. In addition, there is a small piece of the assignment that will introduce you to the rex programming language... . As with Spamsweeper, it's important to get started early -- especially on the applet portion of this assignment!

Example Applet

You can run an example Spampede applet at www.cs.hmc.edu/~dodds/Applets/Spampede/Spampede.html . You may even notice a bug in it (it's not always readily apparent...).

Files and Classes to Create

You will need to modify a Spampede.java file that contains your Spampede class extending Applet. In addition, you will need to use the following classes, which you may spread among several files. Starting points are provided in /cs/cs60/assignments/assignment5.

In addition, the example Spampede applet has files that demonstrate the use of sound, images, and textfiles with an applet and a script for moving things to the appropriate place and setting permissions. You may use or modify these or you may ignore them:
You should copy all of these files to your cs60 directory from /cs/cs60/assignments/assignment5.

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 or in appletviewer. Because the appletviewer instructions were in last week's HW, we mention the browser instructions here:

  1. Make sure you have the Java Console open for your browser for debugging.
  2. Compile everything with javac *.java .
  3. Copy everything to your web directory with ./copyFiles .
  4. 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. Basically, the starting point is the Spampede.java file. You will want to change the Spampede.java file so that you

As you write your code, please compile and rerun the applet often to make sure you're on the right path. Use the Java Console (or appletviewer) to help with debugging and error-detection. Basically, this means many iterations of the compilation, copying, and testing steps above.

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 (spam) that appear at random places on the screen. For each snack consumed, the centipede grows by one segment (a segment is simply one Boardcell). Variations are welcome, but as a default you should implement


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 far beyond...) optional bonus credit.



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 (and much more) are available from the references page. Also, the basics of event handling and Java's built-in classes will be presented in class.

Submission and Testing

There are a lot of files that make up this assignment, so the easiest thing to do is to run


  > cs60submitall
which will submit most of them. If you have images or sound files, they will not be submitted. However, you should just make a note of this at the top of your Spampede.java file and copy those images and sound files (but not source code!) to your ~/public_html directory. The graders will be able to grab them from there.

No tests will be run on your code; instead the graders will play your Spampede applet to see if it does all of the things it should!



Problem 2:     Starting with Rex (20 points)

As a mind-clearing alternative to Spampede, there are a few, very small rex programs to write this week. As you will see, rex is a very different language than java... .

Trying out the rex interpreter

  • Type rex at the prompt to enter the rex interpreter. You can exit rex at any time by hitting the end-of-file character, control-d. Play around with rex and get the feel of how it works.

    For example, you can use rex as an infinite-precision integer calculator:

    rex >  100000000000000000 * 1000000000000000000;
    100000000000000000000000000000000000
    
    rex >  3.141592653589793 * 10;
    31.4159
    
    Rex has double-precision (about 15 places) for floating-point values, but only six places of precision are printed. Note that a semicolon terminates statements in rex. For lots of documentation on rex, see the rex lite reference card.

  • As practice, write a function called add42 that takes a number as input and returns a new number that is 42 greater than the input. Here it is:
    rex >  add42(x) = x + 42;
    1
    
    The 1 basically indicates that rex is happy with your definition of the function add42. So happy, in fact, that you can now use that function:
    rex >  add42(-7);
    35
    
    rex >  add42(190);
    232
    
    You may want to try adding 42 to the string "hello":
    rex >  add42("hello");
    *** warning: can't add non-numerics k
    *** aborting to top-level
    
    As you see, rex will complain if you try to add a string to an integer.

  • Once you've got a little bit of experience with rex, close rex and change to your assignment5 directory or some subdirectory, if you prefer. There, create a file named hw5.rex that will hold your answers for the rex part of this assignment, e.g., using emacs or some other text editor.

    Place your add42 function at the top of the file. You should add a comment with your name, date, and HW number, as well as a comment for your function. (Commenting in rex is identical to Java or C++.) All in all, your file will look something like this

      // CS 60 Homework 0
      // Zach Dodds 
      // 2/19/2004
      
      // Problem 1: add42 is a function that adds 42 to its input argument
    
      add42(x) = x + 42;
    
    Save the file as hw5.rex (you don't need to close emacs, however).

  • At your unix prompt from the directory containing hw5.rex, you can run rex on your file by typing
      >  rex hw5.rex
    
    This will start rex and load in the file. If there are no errors, you will see the rex prompt after a message:
    hw5.rex loaded
    rex >  
    
    Now, you can test your add42 function by hand, as before.

    Alternatively, start rex with just rex and then, at the rex prompt, type *i hw5.rex. This will load in the file (the "*i" stands for "include") and you can now use the functions that were defined in that file.


  • You don't have to test things by hand, though for this assignment it wouldn't be too bad to do so. There is a rex function named test which allows for easy testing of your functions from within a file. Add the lines (even better, copy-and-paste then from this webpage...):
      test( add42(-7), 35 );
      test( add42(0), 41 );
    
    to your hw0.rex file (emacs is still open, right?) Save the file, and in your rex window, kill rex and restart it the same way. You will see the messages
    ok: add42(-7) ==> 35
    bad: add42(0) returned 42, should be 41
    
    This way, for complicated functions, you won't have to retype or recopy tests again and again... . Of course, in this case add42 isn't wrong; instead, the problem is with the second test.

  • The Problems

    You may be worried that you don't know enough about rex to work on HW problems, but read on and (we hope) you'll find these not too bad... . Use the add42 example as a guide!

    Lots more information on using rex is available at the rex summary page. Remember -- this is only a brief introduction to rex -- next week's assignment will start to really explore the language.

    When the problem states that a function's input has a given form, you may assume that the test cases will not take any other forms; that is, you do not need to put in error-checking for erroneous input types.


    Write each of the following functions in your hw5.rex file. Place at least a line of blank space between each problem and use at least one comment line (see the above example of add42 for an example of appropriate commenting).

    You will want to use these operators (perhaps they're familiar?):

       + addition
       - subtraction
       * multiplication
       / division
       % the "mod" or remainder operator
    
    as well as other things you might want to use ... be creative :)

    Submission

    Running cs60submitall will submit your hw5.rex file.