Professional Documents
Culture Documents
Introduction To Greenfoot: The Little Crab: With Thanks To Bruce Chittenden and Michael Kolling
Introduction To Greenfoot: The Little Crab: With Thanks To Bruce Chittenden and Michael Kolling
Introduction To Greenfoot: The Little Crab: With Thanks To Bruce Chittenden and Michael Kolling
Inheritance
The Arrows Denote Hierarchy
Crab is an Animal
Animal is an Actor
Therefore, It Follows That
The Crab is Also an Actor
1
18/01/2013
Make a Crab
2
18/01/2013
Welcome to Java!
3
18/01/2013
/*
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal Type the following line at the
{ pointed to location.
public void act() move();
{
}
}
/*
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
public void act()
{
move();
}
}
4
18/01/2013
5
18/01/2013
Whee...
Crab Panic!
6
18/01/2013
Turning
Turning
7
18/01/2013
8
18/01/2013
/*
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
public void act()
{
move ();
turn (5);
}
}
Give it a try!
9
18/01/2013
Documentation View
Method
Summary
Switch Between
Source and Documentation
10
18/01/2013
Method Signatures
Return Type
Method Name
Parameters
/*
* Test if we are close to one of the edges of the world. Return true if we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
11
18/01/2013
A Crab is an Animal
Then Click on
atWorldEdge()
12
18/01/2013
atWorldEdge now?
Yup!
13
18/01/2013
/*
* This class defines a crab. Crabs live on the beach.
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge ( ) )
{
turn (17);
}
move ( );
}
}
Give it a try!
Crab 2.0!
14
18/01/2013
Crab 2.1
Exercise 2.14
15
18/01/2013
Summary
16
18/01/2013
• Greenfoot.getRandomNumber (20);
• Parameter Limit of 20
• Return a Random Number Range 0 to 19
17
18/01/2013
Randomness
Randomness
18
18/01/2013
Adding Randomness
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* This class defines a crab. Crabs live on the beach..
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge() )
{
turn(17);
}
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn( 5);
}
move();
} Give it a try!
}
19
18/01/2013
Drunken Crab
Adding Randomness
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* This class defines a crab. Crabs live on the beach..
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge() )
{
turn(17);
}
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn( Greenfoot.getRandomNumber(45) );
}
move();
} Give it a try!
}
20
18/01/2013
Drunken Crab
Adding Randomness
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* This class defines a crab. Crabs live on the beach..
*/
public class Crab extends Animal
{
public void act()
{
if ( atWorldEdge() )
{
turn(17);
}
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn( Greenfoot.getRandomNumber(91) - 45 );
}
move();
} Give it a try!
}
21
18/01/2013
Drunken Crab
22
18/01/2013
Don’t forget to
click Compile
all
23
18/01/2013
Eating Worms
24
18/01/2013
Worm Spotting!
public void act()
{
if ( atWorldEdge() )
{
turn(17);
}
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn( Greenfoot.getRandomNumber(91)-45 );
}
move();
if ( canSee (Worm.class) )
{
eat (Worm.class);
}
}
25
18/01/2013
26
18/01/2013
Package java.lang
27
18/01/2013
if ( Greenfoot.getRandomNumber(100) < 10 )
{
turn(5);
}
move();
lookForWorm ();
}
We have to define what this does by adding
in the code we just saw...
28
18/01/2013
Worm Buffet
29
18/01/2013
Worm Buffet
30