can some on help me program my program to do this: write a set of classes that d
ID: 3530337 • Letter: C
Question
can some on help me program my program to do this:
write a set of classes that define the behavior of certain animals.They can be used in a simulation of a world with many animalsmoving around in it. Different kinds of animals will move indifferent ways (you are defining those differences). As thesimulation runs, animals can " die" by ending up in the samelocation, in which case the simulator randomly selects one animalto survive the collision.
The behavior of each animal class is the following:
Class getChar getMove
Bird B Randomly selects one of the fourdirections each time
Frog F Picks a random direction, moves 3 in thatdirection, 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 ( clockwisebox)
Wolf W Has custom behavior that you define.
Your classes should be stored in files called Bird.java, Frog.java,Mouse.java, Turtle.java, and Wolf.java.
Thanks for reading!
Also, the program has to compile, and work.
import java.util.*;
public class critters {
public static void main(String[] args) {
CritterFrame frame = new CritterFrame();
//frame.add(25, Bird.class);
//frame.add(25, Frog.class);
//frame.add(25, Mouse.class);
//frame.add(25, Turtle.class);
//frame.add(25, Wolf.class);
frame.add(25, Stone.class);
frame.start();
}
}
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;
}
}
}
}
}
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 critters
{
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;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.