Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Little Crab Scenario

Similar presentations


Presentation on theme: "The Little Crab Scenario"— Presentation transcript:

1 The Little Crab Scenario

2 Load it Up In: Greenfoot / scenarios / book-scenarios/ little-crab

3 should look like this

4 create a crab object

5 this corresponds to: new Crab( );

6 what can a crab do?

7 some behaviors move( ) boolean atWorldEdge( ) turn( angle )
canSee ( another Class ) Eat (another Class )

8 Invoking methods When invoked (calling a method), returns a TRUE or FALSE as supplied information Whenever you press a number on a keypad or remote control, you are invoking (calling on) a method. e.g. If (key_pressed () == 9 ) { display 9 on screen }

9 Exercise 1 turn (int Angle), when invoked with Angle number, it will turn We'll ask the turn method to turn the crab object 5 degrees.

10

11 Exercise 1 (2) Click the Compile button. If you've typed the statement correctly, you should see the message "Class compiled - no syntax errors" at the bottom of the editor window. If you get an error message instead, examine your code and the code in the screen shot above to make sure it looks EXACTLY the same as my code. Close the editor window and click the Run button (you may need to click the Compile button first). You should see the crab move across the screen and stop at the edge of the world. The statement that calls a method is call a method call. So, move(); is a method call. You must always include the round brackets after the method name and the semi-colon at the end of the line of code. Java is case sensitive, meaning Move(); and move(); are two different statements.

12 Exercise 2 Type the Java statement move(); into the act method of the Crab class To turn the Crab when it reaches the edge of the world, a decision needs to be made. Do you know how to do that?

13 If statements General form of an if statement
If (condition) { //our condition is true instruction; } // if our condition is false, do something something; In plain English: If the condition is true, do instruction, otherwise do something.

14 activity3 Add an if statement into the act() method of the Crab class:
public void act() { if (atWorldEdge()) { turn(10); } move();

15 note pairs of brackets public void? public void act( ) { // this is a behavior! // add behaviors here if ( atWorldEdge( ) ) // most complicated thing here { move(); turn( 10 ); // turn 10 degrees } move( ); Start of if block Most errors can be traced back two things: Semi colons missing at the end of statement; Curly brackets not closed. Remember: For every open { curly bracket you use, it must have one that closes it. } End of if block end act( ) behavior end class

16 brackets and semicolons
brackets surround classes, methods, and if statements classes, methods and if statements don’t use semicolons everything else does

17 Methods - 2 forms public void act( ) { if ( atWorldEdge( ) ) turn( 10 ); } move( ); DEFINITION OR DECLARATION USE, CALL, OR INVOCATION

18 code! /** * This class defines a crab. Crabs live on the beach. */
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal // definition statement { public void act( ) // this is a behavior! // add behaviors here if ( atWorldEdge( ) ) // most complicated thing here turn( 10 ); } move();

19 /. This class defines a crab. Crabs live on the beach
/* This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal // definition statement To be freely used what we’re designing what it’s part of

20 Summary We are adding code to the act( ) method methods = “behaviors”
public void act( ) method “declaration” turn( 17 ); method “call” move( ); method “call” if statement with brackets


Download ppt "The Little Crab Scenario"

Similar presentations


Ads by Google