Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment will give you practice with defining classes. You are to write a

ID: 3725363 • Letter: T

Question

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. 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. On each round of the simulation, each critter is asked which direction it wants to move. Each critter can move north, south, east or west or can stay on the current location by saying that the direction it wants is “center.” 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. Each of your classes will implement the Critter interface, shown below. // 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 must return a legal move (one of // the constants NORTH, SOUTH, EAST, WEST, CENTER). See the // CritterInfo interface for a description of its methods. public interface Critter { public char getChar(); public int getMove(CritterInfo info); public static final int NORTH = -1; public static final int SOUTH = 3; public static final int EAST = 8; public static final int WEST = 11; public static final int CENTER = 42; } 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 { } 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). For example, below is a definition for a class called Stone that is part of the simulation. Stones are displayed with the letter S and always stay on the current location (returning CENTER for their move). public class Stone implements Critter { public char getChar() { return 'S'; } public int getMove(CritterInfo info) { return CENTER; } } 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 a constructor for your classes if you want, although it must be a zeroargument constructors (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 You decide You define this For the random moves, each possible choice must be equally likely. You may use either a Random object or the Math.random() method to obtain pseudorandom values. The first four of these classes won’t use the CritterInfo object that is passed to getMove. This object is provided in case you want to do something with this information in defining your Wolf class. See the description of CritterInfo later in the writeup. 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 the Critter class defines five 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) because you are implementing the interface. 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. As noted above, your critter can stay on its current location by indicating that the direction it wants to move is CENTER. Every time your critters are asked to move, they are passed an object of type CritterInfo that will provide information about that particular critter. For example, you can find out the critter’s current x and y coordinates by calling getX() and getY(). You can find out what is around the critter by calling getNeighbor using one of the direction constants. You will be told the display character for whatever is in that location (a period for an empty cell). The CritterInfo interface is defined as follows: // // The CritterInfo interface defines a set of methods for querying the // state of a critter simulation. The getX and getY methods return a // critter's current location in the world. The getNeighbor method // takes a direction as a parameter (one of the constants NORTH, // SOUTH, EAST, WEST or CENTER from the Critter interface). It // returns the display character for the critter that is one unit away // in that direction (a period if the square is empty). The getHeight // and getWidth methods return the height and width of the world. public interface CritterInfo { public int getX(); public int getY(); public char getNeighbor(int direction); public int getHeight(); public int getWidth(); } Notice that you are asked to define the Wolf class yourself, including the display character. Critters are asked what display character to use on each round of the simulation, so you can have a critter that displays itself differently over time.

Explanation / Answer

import java.util.*;

interface animal //Interface having move comman to all animal

{

public void move();

}

class Bird implements animal

{

Random g;

public int x,y;

Bird()

{

x=y=0;

g=new Random();

}

public void move()

{

int d;

d=g.nextInt(4); //Randomly select direction form 0 to 3.

switch(d)

{

case 0: //Direction east

x++;

break;

case 1: //Direction north

y++;

break;

case 2: //Dirction west

x--;

break;

case 3: //Direction south

y--;

break;

}

System.out.println("Bird has move to "+x+", "+y);

}

}

class Frog implements animal

{

Random g;

public int x,y,n;

int d;

Frog()

{

n=x=y=0;

g=new Random();

}

public void move()

{

n++;

if(n==0)

d=g.nextInt(4);

else

if(n>4) //remain in same direction until n reaches to 0 .

n=0;

switch(d)

{

case 0:

x+=3;

break;

case 1:

y+=3;

break;

case 2:

x-=3;

break;

case 3:

y-=3;

break;

}

System.out.println("Frog has move to "+x+", "+y);

}

}

class Mouse implements animal

{

public int x,y;

int d;

Mouse()

{

x=y=0;

d= 2;

}

public void move()

{

if(d==2)

d=1; //change to north direction

else

d=2; //change to west direction

switch(d)

{

case 1:

y++;

break;

case 2:

x--;

break;

}

System.out.println("Mouse has move to "+x+", "+y);

}

}

class Turtle implements animal

{

public int x,y;

int d;

Turtle()

{

x=y=0;

d= 0;

}

public void move()

{

if(d==0)

d=3;

else

d--; //direction goes from 3 to 0 then again 3 to 0.

switch(d)

{

case 0:

x+=5; //move step 5

break;

case 1:

y+=5;

break;

case 2:

x-=5;

break;

case 3:

y-=5;

break;

}

System.out.println("Turtle has move to "+x+", "+y);

}

}

class Wolf implements animal

{

Random g;

public int x,y;

int d;

Wolf()

{

x=y=0;

g=new Random();

d= 0;

}

public void move()

{

d=g.nextInt(3); //select direction east west & north.

switch(d)

{

case 0:

x++;

break;

case 1:

y++;

break;

case 2:

x--;

break;

case 3:

y--;

break;

}

System.out.println("Wolf has move to "+x+", "+y);

}

}

class world

{

public static void main(String args[])

{

int i=0,alive;

boolean bird_live=true; //flag to idicate bird is alive.

boolean frog_live=true;

boolean mouse_live=true;

boolean turtle_live=true;

boolean wolf_live=true;

Random g = new Random();

Bird b = new Bird();

Frog f = new Frog();

Mouse m =new Mouse();

Turtle t = new Turtle();

Wolf w = new Wolf();

for(i=0;i<15;i++)

{

if(bird_live)

b.move();

if(frog_live)

f.move();

if(mouse_live)

m.move();

if(turtle_live)

t.move();

if(wolf_live)

w.move();

System.out.println();

if(b.x == f.x && b.y == f.y && bird_live && frog_live) //collision detection

{

bird_live=false;

frog_live=false;

System.out.println("-----> bird frog collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive bird");

bird_live=true;

}

else

{

System.out.println("------> simulator chose to alive frog");

frog_live=true;

}

}

if(b.x == m.x && b.y == m.y && bird_live && mouse_live)

{

bird_live=false;

mouse_live=false;

System.out.println("-----> bird mouse collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive bird");

bird_live=true;

}

else

{

System.out.println("------> simulator chose to alive mouse");

mouse_live=true;

}

}

if(b.x == t.x && b.y == t.y && bird_live && turtle_live)

{

bird_live=false;

turtle_live=false;

System.out.println("-----> bird turtle collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive bird");

bird_live=true;

}

else

{

System.out.println("------> simulator chose to alive turtle");

turtle_live=true;

}

}

if(b.x == w.x && b.y == w.y && bird_live && wolf_live)

{

bird_live=false;

wolf_live=false;

System.out.println("-----> bird wolf collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive bird");

bird_live=true;

}

else

{

System.out.println("------> simulator chose to alive wolf");

wolf_live=true;

}

}

if(f.x == m.x && f.y == m.y && frog_live && mouse_live)

{

frog_live=false;

mouse_live=false;

System.out.println("-----> frog mouse collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive frog");

frog_live=true;

}

else

{

System.out.println("------> simulator chose to alive mouse");

mouse_live=true;

}

}

if(f.x == t.x && f.y == t.y && frog_live && turtle_live)

{

frog_live=false;

turtle_live=false;

System.out.println("-----> frog turtle collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive frog");

frog_live=true;

}

else

{

System.out.println("------> simulator chose to alive turtle");

turtle_live=true;

}

}

if(f.x == w.x && f.y == w.y && frog_live && wolf_live)

{

frog_live=false;

wolf_live=false;

System.out.println("-----> frog wolf collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive frog");

frog_live=true;

}

else

{

System.out.println("------> simulator chose to alive wolf");

wolf_live=true;

}

}

if(m.x == t.x && m.y == t.y && mouse_live && turtle_live)

{

mouse_live=false;

turtle_live=false;

System.out.println("-----> mouse turtle collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive mouse");

mouse_live=true;

}

else

{

System.out.println("------> simulator chose to alive turtle");

turtle_live=true;

}

}

if(m.x == w.x && m.y == w.y && mouse_live && wolf_live)

{

mouse_live=false;

wolf_live=false;

System.out.println("-----> mouse wolf collision");

alive=g.nextInt(2);

if(alive ==0)

{

System.out.println("------> simulator chose to alive mouse");

mouse_live=true;

}

else

{

System.out.println("------> simulator chose to alive wolf");

wolf_live=true;

}

}

}

}

}

/* Output may be similar to this ....

Bird has move to 0, 1

Frog has move to 3, 0

Mouse has move to 0, 1

Turtle has move to 0, -5

Wolf has move to 1, 0

-----> bird mouse collision

------> simulator chose to alive bird

Bird has move to 0, 0

Frog has move to 6, 0

Turtle has move to -5, -5

Wolf has move to 1, 1

Bird has move to 1, 0

Frog has move to 9, 0

Turtle has move to -5, 0

Wolf has move to 2, 1

Bird has move to 2, 0

Frog has move to 12, 0

Turtle has move to 0, 0

Wolf has move to 1, 1

Bird has move to 2, -1

Frog has move to 15, 0

Turtle has move to 0, -5

Wolf has move to 2, 1


Bird has move to 3, -1

Frog has move to 18, 0

Turtle has move to -5, -5

Wolf has move to 2, 2


Bird has move to 2, -1

Frog has move to 21, 0

Turtle has move to -5, 0

Wolf has move to 2, 3


Bird has move to 2, 0

Frog has move to 24, 0

Turtle has move to 0, 0

Wolf has move to 3, 3


Bird has move to 2, -1

Frog has move to 27, 0

Turtle has move to 0, -5

Wolf has move to 2, 3


Bird has move to 3, -1

Frog has move to 30, 0

Turtle has move to -5, -5

Wolf has move to 3, 3


Bird has move to 2, -1

Frog has move to 33, 0

Turtle has move to -5, 0

Wolf has move to 3, 4


Bird has move to 2, -2

Frog has move to 36, 0

Turtle has move to 0, 0

Wolf has move to 3, 5


Bird has move to 2, -1

Frog has move to 39, 0

Turtle has move to 0, -5

Wolf has move to 2, 5


Bird has move to 2, -2

Frog has move to 42, 0

Turtle has move to -5, -5

Wolf has move to 3, 5


Bird has move to 1, -2

Frog has move to 45, 0

Turtle has move to -5, 0

Wolf has move to 3, 6

*/