Chapter 2 - The First Program: Little Crab

advertisement
Chapter 2 –
The Little Crab
Program:
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
Little Crab Scenario
Click Run and observe what
happens
Little Crab Scenario
Right Click the Crab Class
Click On Open Editor
Little Crab Scenario
When Run is Clicked
the Crab does nothing
This is because there is no Source
Code in the act Method for the
Crab.
Moving an Actor
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/*
* This class defines a crab. Crabs live on the beach.
*/
Replace the comment
public class Crab extends Animal
with move();
{
public void act()
{
// Add your action code here
}
}
Moving an Actor
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()
{
Causes the crab to move
move();
5 pixel in the current direction.
}
}
Compiling your code
After adding the call to move( );
click Compile
Running your code
Right Click on the Crab
Class
Drag a Crab into the World
Click the Act and Run
Buttons
Running your code
Crab Moves to Edge of the World
Running your code
Place Multiple Crabs into the
World
Click the Act and Run Buttons
Running your code
Crabs All Move to Edge of the
World
Turning an Actor
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()
{
Replace move() with
turn(5);
turn(5) and compile.
}
}
Turning an Actor
Crab Spins to the Right
Turning an Actor
Set turn to -5 and recompile
Turning an Actor
Crab Spins to the Left
Turning an Actor
The parameter given to the
turn() method defines the
change of direction per call
to the act() method. The
value is interpreted as an
arcdegree.
A positive value causes
clockwise spin and a
negative value
counter-clockwise spin.
Moving and Turning
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);
}
}
Moving and Turning
move and turn
Moving and Turning
Crab moves in a circle
Errors
Syntax Error - Expected
Semicolon
Various Errors
1.
‘;’ expected (Terminator symbol)
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
11.
reached end of file while parsing
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Various Errors
Method Signatures
Return Type
Method Name
Parameters
void turn (int angle)
boolean atWorldEdge ( )
void move ( )
All these methods are technically part of the Animal superclass.
The Crab subclass is able to use them due to the process called
Inheritance (a Crab is an Animal an can thus act like one).
To see what else an Animal can do, we can check its Documentation
Documentation View
Right Click on Animal Class
and Click on Open editor
Documentation View
Method
Summary
Switch Between
Source and Documentation
Documentation View
Running A Method
Right Click on the Crab and
Select inherited from Animal
Then Click on
atWorldEdge ( )
Getting A Return Value
Running A Method
Move the Crab to the Edge of
the World and Repeat the
Experiment
Getting A Return Value
Turning at-the-world-edge
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 ( );
}
}
Turning at-the-world-edge
The Crab turns only if it is in
contact with the world edge
A Logic Error
Place the move () Inside the
if Statement
A Logic Error
If the Crab is at the Edge, Then It Turns
Around.
If the Crab is Not Near the Edge Nothing
Happens.
Staying off Edges
Crab should run around the
world, peforming a complete
turn once it hits a wall.
Reversing Direction
Set the Turn to 180 and the
Crab Should Reverse Direction
Reversing Direction
Crab Moves from Side to Side
Introducing Variables
In the previous example, we made the Crab
turn around whenever it collides with the border.
Let us assume that we want to implement a way to
turn around without using the border as a stopping
point.
Instead, we want the crab to turn around after
every 40th step (Step 40, Step 80, Step 120 …).
Introducing Variables
Does not help us in this
situations
Introducing Variables
We obviously need a way to count “steps”
(a step is 1 call to the move() method, i.e. 5 pixel).
This requires that we implement a “data field”
to store the current number of steps and
count up by one for each step taken.
Such a data field is called a Variable:
int stepCount = 0
Introducing Variables
We obviously need a way to count steps.
This requires that we implement a “data field”
to store the current number of moves and count up
by one for each step taken.
Such a data field is called a Variable:
int stepCount = 0
Type
Introducing Variables
We obviously need a way to count steps.
This requires that we implement a “data field”
to store the current number of moves and count up
by one for each step taken.
Such a data field is called a Variable:
int stepCount = 0
Type
Name
Introducing Variables
We obviously need a way to count steps.
This requires that we implement a “data field”
to store the current number of moves and count up
by one for each step taken.
Such a data field is called a Variable:
int stepCount = 0
Type
Name
Initial
Value
Introducing Variables
Note the placement of the variables
(outside of the move method).
It does not belong to a specific
method, but to the entire class.
Introducing Variables
The algorithm for turning around after every
40th step:
1. Increase stepCount by 1.
2. If stepCount is equal to 40,
then set stepCount to 0 and turn around.
3. Take a step.
Introducing Variables
Increase stepCount by 1
Check if we are taking a 40th step
Reset stepCount and turn around
Take a step
Summary of Programming
Techniques
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.
Concept Summary
Download