Monday, March 30, 2009

Java Codes For CRAB & LOBSTER Subclasses


.




.



Hello Friends!

The following is the code for crab and Lobester which is nothing but a set of instructions written in Java and the creatures react to these instruction.

Code for Crab :

public class lobster extends Animal

{

/*** Act - do whatever the lobster wants to do. This method is called whenever* the 'Act' or 'Run' button gets pressed in the environment.*/

public void act()
{
if (atWorldEdge())
{
turn(17);
}
move();
if (Greenfoot.getRandomNumber(100) > 90)
{
turn(Greenfoot.getRandomNumber(90)-45);
}
if (canSee(Crab.class))
{
eat(Crab.class);
}
}
}


Code for Lobster :

public class lobster extends Animal
{
/*** Act - do whatever the lobster wants to do. This method is called whenever* the 'Act' or 'Run' button gets pressed in the environment.*/

public void act()
{
if (atWorldEdge())
{
turn(17);
}
move();
if (Greenfoot.getRandomNumber(100) > 90)
{
turn(Greenfoot.getRandomNumber(90)-45);
}
if (canSee(Crab.class))
{
eat(Crab.class);
}
}
}

By entering this code we can make lobster and crab to behave according to the game and we can also make appropriate changes to the code whenever neccessary.

Thanks
Raheel Ahmed.



.

No comments:

Post a Comment