18/01/2013
Introduction to GreenFoot: The
Little Crab
With thanks to Bruce Chittenden and
Michael Kolling
The Little Crab!
Inheritance
The Arrows Denote Hierarchy
Crab is an Animal
Animal is an Actor
Therefore, It Follows That
The Crab is Also an Actor
Click Compile all
1
18/01/2013
Make a Crab
Right-click on Crab and select
“new Crab()”
Find a spot on the sand to place
the crab.
Run Your Program
Click Run and Nothing Happens
2
18/01/2013
Let’s Make Some Changes
Right Click the Crab Class
Click On Open Editor
Welcome to Java!
When Run is Clicked
the Crab does nothing
This is because there is no Source Code in
the act Method for the Crab.
3
18/01/2013
Making the Crab Move
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* 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();
{
}
}
Making the Crab Move
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()
{
move();
}
}
4
18/01/2013
Almost Ready To Go...
Add the call to move( );
Click Compile
Make sure it says “no syntax
errors” before proceeding. If
it does check to make sure
that what you typed matches
the above exactly.
“Act” moves one step at a time
“Run” moves continuously
Right Click on the Crab Class
Drag a Crab into the World
Click the Act and Run Buttons
5
18/01/2013
Whee...
Crab Moves to Edge of the World
Crab Panic!
Place Multiple Crabs into
the World
Click the Run Buttons
Click the Rest Button to
clear the old crabs and start
over
6
18/01/2013
Turning
public void act ()
{
turn (5);
}
Turning
Set turn to 5 and recompile
7
18/01/2013
Whoa, I’m Gonna Barf...
Crab Spins to the Right
Now What Happens?
Set turn to -5 and recompile
8
18/01/2013
What Does This Do?
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()
{
move ();
turn (5);
}
}
Give it a try!
Dealing with Screen Edges
Right Click on Animal Class
and Click on Open editor
9
18/01/2013
Documentation View
Method
Summary
Switch Between
Source and Documentation
This is What an Animal Can Do
10
18/01/2013
Method Signatures
Return Type
Method Name
Parameters
• void turn (int angle)
• boolean atWorldEdge ( )
• void move ( )
Source for atWorldEdge( )
/*
* 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
Right Click on the Crab and Select
inherited from Animal
Then Click on
atWorldEdge()
Boolean: true or false
12
18/01/2013
atWorldEdge now?
Move the Crab to the Edge of the
World and Repeat the Experiment
Yup!
13
18/01/2013
React to World Edge Now
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);
}
move ( );
}
}
Give it a try!
Crab 2.0!
Crab Runs Around the World Turning when
it Encounters a Wall
14
18/01/2013
Crab 2.1
Set the Turn to 180 and the Crab
Should Reverse Direction
Exercise 2.14
Place the move () Inside the
if Statement
15
18/01/2013
Summary
If the Crab is Not Near the Edge
Nothing Happens.
If the Crab is at the Edge, Then It
Turns Around.
What Have We Learned?
In this chapter, we have seen how to call methods such as
move( ), with and without parameters. This will form the
basis for all further Java Programming.
We have encountered a glimpse of inheritance. Classes
inherit the methods from their superclasses.
And, very important we have seen how to make decisions.
We have used an if-statement for conditional execution.
16
18/01/2013
Let’s try Crab 3.0!
Adding Random Behaviour
• The Greenfoot Environment has a Method to
Give a Random Number.
• Greenfoot.getRandomNumber (20);
• Parameter Limit of 20
• Return a Random Number Range 0 to 19
17
18/01/2013
Randomness
if ( Greenfoot.getRandomNumber (100) < 10)
{
// Code Here Executes 10% of the Time
}
Randomness
if ( Greenfoot.getRandomNumber (100) < 7)
{
// Code Here Executes 7% of the Time
}
18
18/01/2013
Java Comparison Operators
• < less than
• <= less than or equal
• == equal
• != not equal
• >= greater than or equal
• > greater than
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
Crab Randomly 10% of the time Turns
Crab Turns Only to the Right
Crab Turns 5 Degrees When it Turns
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
Crab Randomly 10% of the time Turns
Crab Turns Only to the Right
Crab Randomly Turns Between
0 and 44 Degrees When it Turns
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
Crab Randomly 10% of the time
Turns
Crab Turns Both Left and Right
Crab Randomly Turns Between -45
and 45 Degrees When it Turns
Let’s Add Worms
Right Click on the Animal Class
and then Click on New subclass
22
18/01/2013
Let’s Add Worms
New Animal Class Worms
Don’t forget to
click Compile
all
23
18/01/2013
Crab Meet Worm
Crabs Move Randomly
Worms Do Nothing
Crabs Ignore the Worms
Eating Worms
boolean canSee (java.lang.Class clss)
void eat (java.lang.Class clss)
24
18/01/2013
canSee and eat
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
Crabs Eat the Worms
Java Library Documentation
Click on Java Library Documentation
Look for java.lang.class
26
18/01/2013
Java 2 Platform Packages
Package java.lang
27
18/01/2013
Creating New Behaviours Through
Methods
/*
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
}
}
Add it to Our Crab Behaviour
public void act()
{
if ( atWorldEdge() )
{
turn(17);
}
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
We Need To Add This To Crab
/*
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
}
}
Worm Buffet
Place Some Crabs and Worms
in the World
29
18/01/2013
Worm Buffet
Eventually the Crabs Eat All the
Worms
30