Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSE142 —Computer Programming I

Programming Assignment #8

Please note that solutions to this homework will not be accepted after 11 pm on Saturday, June 4th . 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  wandering around in it .  Different kinds of animals will behave in different ways and you are defining those       differences .  In this world, animals propagate their species by infecting other animals with their DNA, which     transforms the other animal into the infecting animal’s species .  This idea of animals transforming into a            different species appeared in many Star Trek episodes, particularly the Next Generation episode called “Identity Crisis .”

For this assignment you will be given a lot of supporting code that runs the simulation .  You are just defining the individual “critters” that wander around this world trying to infect each other .  While it is running the       simulation will look something like this:

Each of your critter classes should extend a class known as Critter .  So each Critter class will look like this:

public class SomeCritter extends Critter {

...

}

The “extends” clause in the header of this class establishes an inheritance relationship .  This is discussed in        chapter 9 of the textbook, although you don’t need a deep understanding of it for this assignment .  The main       point to understand is that the Critter class has several methods and constants defined for you .  So by saying that you extend the class, you automatically get access to these methods and constants .  You then give new                definitions to certain methods to define the behavior of your critters .

On each round of the simulation, each critter is asked what action it wants to perform .  There are four possible responses each with a constant associated with it .

Constant

Description

Action.HOP

Move forward one square in its current direction

Action.LEFT

Turn left (rotate 90 degrees counter-clockwise)

Action.RIGHT

Turn right (rotate 90 degrees clockwise)

Action.INFECT

Infect the critter in front of you

There are three key methods in the Critter class that you will redefine in your own classes .  When you redefine these methods, you must use exactly the same method header as what you see below .  The three methods to     redefine for each Critter class are:

public Action getMove(CritterInfo info) {

}   ...

public Color getColor() {

}   ...

public String toString() {

}   ...

For the getMove method you should return one of the four Action constants described earlier in the writeup .  For the getColor method, you should return whatever color you want the simulator to use when drawing your critter .  And for the toString method, you should return whatever text you want the simulator to use when      displaying your critter (normally a single character) .

For example, below is a definition for a critter that always infects and that displays itself as a green letter F: import java.awt.*;

public class Food extends Critter {

public Action getMove(CritterInfo info) {

return Action.INFECT;

}

public Color getColor() {

return Color.GREEN;

}

public String toString() {

return "F";

}

}

Notice that it begins with an import declaration to be able to access the Color class .  All of your Critter classes will have the basic form shown above .

The getMove method is passed an object of type CritterInfo .  This is an object that provides you information  about the current status of the critter .  It includes eight methods for asking about surrounding neighbors plus a method to find out the current direction the critter is facing .  Below are the methods of the CritterInfo class:

CritterInfo Method

Description

public Neighbor getFront()

returns neighbor in front of you

public Neighbor getBack()

returns neighbor in back of you

public Neighbor getLeft()

returns neighbor to your left

public Neighbor getRight()

returns neighbor to your right

public Direction getDirection()

returns direction you are facing

public boolean frontThreat ()

whether there is an enemy facing you in front of you

public boolean backThreat ()

whether there is an enemy facing you in back of you

public boolean leftThreat ()

whether there is an enemy facing you to your left

public boolean rightThreat ()

whether there is an enemy facing you to your right

The getDirection method tells you what direction you are facing (one of four direction constants):

Constant

Description

Direction.NORTH

facing north

Direction.SOUTH

facing south

Direction.EAST

facing east

Direction.WEST

facing west

The return type for the first group of four CritterInfo methods is Neighbor .  There are four different constants for the different kind of neighbors you might encounter:

Constant

Description

Neighbor.WALL

The neighbor in that direction is a wall

Neighbor.EMPTY

The neighbor in that direction an empty square

Neighbor.SAME

The neighbor in that direction is a critter of your species

Neighbor.OTHER

The neighbor in that direction is a critter of another species

Notice that you are only told whether critters are of your species or some other species .  You can’t find out    exactly what species they are .  The second group of four methods tells you whether there are enemies in each direction who are facing you, which means they could potentially infect you .

This program will probably be confusing at first because this is the first time where you are not writing the main method .  Your code will not be in control .  Instead, you are defining a series of objects that become part of a      larger system .  For example, you might find that you want to have one of your critters make several moves all at once .  You won’t be able to do that .  The only way a critter can move is to wait for the simulator to ask it for a   move .  The simulator is in control, not your critters .  Although this experience can be frustrating, it is a good      introduction to the kind of programming we do with objects .  A typical Java program involves many different    interacting objects that are each a small part of a much larger system .


Critters move around in a world of finite size that is enclosed on all four sides by walls .  You can include a     constructor for your classes if you want, although it should generally be a zero-argument constructor (one that takes no arguments) .  The one exception is that you are to define a class called Bear that takes a boolean         parameter specifying whether it is a black bear or a polar bear, which will change how its color is displayed .   The simulator treats bears in a special way, basically flipping a coin each time it constructs one to decide        whether or not to create a white bear or black bear .  Your Bear class does not have to figure this out .  You just

have to make sure that each bear pays attention to the boolean value passed to the constructor by the simulator .

You are to implement four classes .

Bear

Constructor

public Bear(boolean polar)

getColor

Color.WHITE for a polar bear (when polar is true), Color.BLACK otherwise (when polar is false)

toString

Should alternate on each different move between a slash character (/) and a backslash character (\) starting with a slash.

getMove

always infect if an enemy is in front

otherwise hop if possible

otherwise turn left .

Lion

Constructor

public Lion()

getColor

Randomly picks one of three colors (Color.RED, Color.GREEN, Color.BLUE) and uses that color for three moves, then randomly picks one of those colors again for the next three moves, then randomly picks another one of those colors for the next three moves, and so on.

toString

"L"

getMove

always infect if an enemy is in front

otherwise if a wall is in front or to the right, then turn left

otherwise if a fellow Lion is in front, then turn right

otherwise hop .