handout #26

CSE142—Computer Programming I

Programming Assignment #8

due: Tuesday, 12/6/05, 2 pm

This assignment will give you practice with defining classes.  You are to write a set of classes that define the behavior of certain animals.  You will be given 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).  The x/y values passed to getMove represent the

// critter’s current location in the world.

 

public interface Critter {

    public char getChar();

 

    public int getMove(int x, int y);

 

    public static final int NORTH = -1;

    public static final int SOUTH = 3;

    public static final int EAST = 8;

    public static final int WEST = 11;

}

Interfaces are discussed in detail in chapter 9 of the textbook, but to do this assignment you just need to know a few simple rules about interfaces.  Your class headers should indicate that they implement this interface, as in:

public class Bird implements Critter {

    <class definition>

}

Because you are implementing the Critter interface, you will be able to refer to the class constants directly (e.g., NORTH).  The other important detail about interfaces is that you must include in each class a definition for the two methods in the interface (the getChar method and the getMove method).

Critters move around in a world of finite size, but the word is toroidal (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).

You are allowed to include constructor methods for your classes if you want, although any constructor that you define has to be a zero-argument constructor (one that takes no arguments).

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 the Math.random() method to obtain pseudorandom values.

The first four of these classes won’t use the (x, y) coordinates that are passed to getMove.  This information is provided in case you want to do something with this information in defining your Wolf class.  The critter world is divided into cells that have integer coordinates, much like the pixels in a DrawingPanel.  There are 100 cells across and 50 cells up and down.  As with the DrawingPanel, the upper-left cell has coordinates (0, 0), increasing x values move you right and increasing y values move you down.

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.  Any data fields (state variables) of your classes should be declared using the “private” keyword.

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, CritterPanel.java and CritterMain.java in the same folder as your program to run the GUI.  You should open and compile CritterMain to run the program.  The files that you need will be included in a zip folder called ass8.zip available from the class web page.  You should download and unzip this folder, then write your five classes, compile CritterMain and run.