02a LittleCrabProgram

Report
Program: Little Crab
Mr Gano
Little Crab Scenario
Inheritance
The Arrows Denote Hierarchy
Crab is an Animal
Animal is an Actor
Therefore, It Follows That
The Crab is Also an Actor
Crab Game
Start Greenfoot and open the little-crab scenario. Place a crab into the
world and run the program (click the Run button). What do you
observe? (Remember: If the class icons on the right appear striped, you
have to compile the project first.)
Press the Run button and nothing happens. This is due to the fact that
there are no instructions in the act() method.
Click Run and Nothing Happens
Open the source code for the Crab object by right clicking
on the Crab object and selecting Open editor.
Right Click the Crab Class
Click On Open Editor
Here we can see that the act() method is empty. This means there are no
instructions for the crab and that is why the crab does nothing when
told to act.
When Run is Clicked
the Crab does nothing
This is because there is no Source Code in the
act Method for the Crab.
Making the Crab Move
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* This class defines a crab. Crabs live on the beach.
*/
Replace the comment with
public class Crab extends Animal
move();
{
public void act()
{
// Add your action code here
}
}
move() method added to the act() method
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();
}
}
Compile after editing the act() method
Add the call to move( );
Click Compile
Place a crab in the world
Right Click on the Crab Class
Drag a Crab into the World
Click the Act and Run Buttons
Click Run and the crab moves to the end of the world
Crab Moves to Edge of the World
More than one crab
Place Multiple Crabs into the World
Click the Act and Run Buttons
All crabs hit the edge of the world
Crabs All Move to Edge of the World
Turning
public void act ()
{
turn (5);
}
Work with the turn() method to get the crab to make a turn
Set turn to 5 and recompile
Crab spins to the right with a positive integer
Crab Spins to the Right
Try another parameter to see what the crab will do.
Set turn to -5 and recompile
Crab spins to the left with a negative integer
Crab Spins to the Left
Code
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);
}
}
Methods instructing crab to move and turn
move and turn
The result of the code here is the crab turns in a circle
Crab moves in a circle
Syntax error message
Syntax Error - Expected Semicolon
Try to cause more errors
Make various changes to cause different error messages. Find at least
five different error messages. Write down each error message and what
change you introduced to provoke this error.
1. ‘;’ expected
2. cannot find symbol - method Move()
3. turn(int) in Animal cannot be applied to (int,int)
4. turn(int) in Animal cannot be applied to (double)
5. act() in Crab cannot override act() in Animal; attempting to use
incompatible return type
6. cannot find symbol - class voids
7. unclosed comment
8. class, interface, or enum expected
9. illegal start of type
10. illegal start of expression
Dealing with Screen Edges
Right Click on Animal Class
and Click on Open editor
Documentation View
Method
Summary
Switch Between
Source and Documentation
Documentation View
Open the editor for the Animal class. Switch to Documentation view.
Find the list of methods for this class (the “Method Summary”). How
many methods does this class have?
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 is 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;
}
Find boolean atWorldEdge() method
Create a crab. Right-click it, and find the boolean atWorldEdge()
method (it is in the inherited from Animal submenu, since the crab
inherited this method from the Animal class). Call this method. What
does it return?
Right click to find the atWorldEdge() method by selecting
‘inherited from Animal’ and then clicking on ‘boolean
atWorldEdge()
Right Click on the Crab and
Select inherited from Animal
Then Click on
atWorldEdge ( )
Here we can see the boolean value that is returned
from the method atWorldEdge()
See what boolean value is returned from atWorldEdge()
when the crab is at the edge of the world
Move the Crab to the Edge of the
World and Repeat the Experiment
Here we see the value returned as true when we
check on the atWorldEdge() method
Code for setting conditions of atWorldEdge() method.
This condition tells the turtle to turn 17 degrees when it
hits the edge of the world
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 ( );
}
}
Make a crab turn when it hits the edge of the world
Crab Runs Around the World
Turning when it Encounters a Wall
Set the Turn to 180 and the Crab
Should Reverse Direction
Crab Moves from Side to Side
Place the move () Inside the
if Statement
We now see the crab following its given instructions
If the Crab is Not Near the Edge Nothing Happens.
If the Crab is at the Edge, Then It Turns Around.
Summary of Programming Techniques
In this PPT, 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.
Concept Summary
A method call is an instruction that tells an object to perform an action. The action is
defined by a method of the object.
Additional information can be passed to some methods within the parentheses. The value
passed is called a parameter.
Multiple instructions are executed in sequence, one after the other, in the order in which
they are written.
When a class is compiled, the compiler checks to see whether there are any errors. If an
error is found, an error message is displayed.
A subclass inherits all the methods from its superclass. That means that it has, and can
use, all methods that its superclass defines.
Calling a method with a void return type issues a command. Calling a method with a
non-void return type asks a question.
An if-statement can be used to write instructions that are executed only when a certain
condition is true.

similar documents