handout #30

CSE142—Computer Programming I

Programming Assignment #9

due: Friday, 12/10/04, 5 pm

This assignment will give you practice with defining classes that implement an interface.  You are to write a set of classes that define the behavior of certain animals.  Stuart has written a program that runs a simulation of a world with many animals moving around in it.  Different kinds of animals will move in different ways and you are defining those differences.  As the simulation runs, animals can “die” by ending up on the same location, in which case the simulator randomly selects one animal to survive the collision.

Each of your classes will implement the Critter interface, shown below.

// Stuart Reges

// 12/03/04

//

// The Critter interface defines the methods necessary for an animal to

// participate in the critter simulation.  It must return a character

// when getChar is called that is used for displaying it on the screen.

// The getMove method returns a legal move (one of the constants NORTH,

// SOUTH, EAST, WEST).

 

public interface Critter {

    public char getChar();

 

    public int getMove();

 

    public static final int NORTH = 0;

    public static final int SOUTH = 1;

    public static final int EAST = 2;

    public static final int WEST = 3;

}

You are to implement five classes.  The behavior of each class is described below.

Class

getChar

getMove

Bird

B

Randomly selects one of the four directions each time

Frog

F

Picks a random direction, moves 3 in that direction, repeat (same as bird, but staying in a single direction longer)

Mouse

M

West 1, north 1, repeat (zig zag to the NW)

Turtle

T

South 5, west 5, north 5, east 5, repeat (clockwise box)

Wolf

W

You define this

For the random moves, each possible choice must be equally likely.  Use either a Random object or the Math.random() method to obtain pseudorandom values.

Critters move around in a world of finite size, but the word is torodial (going off the end to the right brings you back to the left and vice versa; going off the end to the top brings you back to the bottom and vice versa).

Notice that you are asked to define the Wolf class yourself.  Four of the style points for this assignment will be awarded on the basis of how much energy and creativity you put into defining an interesting class.  These four points will be much harder to earn than the other 16 points for the assignment, so it really is a way for us to reward the students who decide to spend time figuring out an interesting critter definition.

Notice that the Critter class defines four constants for the various directions.  You can refer to these using the name of the interface (Critter.NORTH, Critter.SOUTH, etc) or you can refer to them directly (NORTH, SOUTH, etc).  Your code should not depend upon the specific values assigned to these constants, although you may assume they will always be of type int.  You will lose style points if you fail to use the named constants when appropriate.

The rest of the style points will be awarded on the basis of your use of good comments and variable names and your ability to express these operations simply and clearly.

Your classes should be stored in files called Bird.java, Frog.java, Mouse.java, Turtle.java and Wolf.java.  You will need to include the files Critter.java, CritterModel.java, CritterFrame.java and CritterMain.java from the class web page (under the “assignments” link) in the same folder as your program to run the GUI.  You should open and compile CritterMain to run the program.