Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.

Similar presentations


Presentation on theme: "Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It."— Presentation transcript:

1 Chapter 2 – The Little Crab Program:

2 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

3 Little Crab Scenario Click Run and observe what happens

4 Little Crab Scenario Right Click the Crab Class Click On Open Editor

5 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.

6 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() { // Add your action code here } Replace the comment with move();

7 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() { move(); } Causes the crab to move 5 pixel in the current direction.

8 Compiling your code After adding the call to move( ); click Compile

9 Running your code Right Click on the Crab Class Drag a Crab into the World Click the Act and Run Buttons

10 Running your code Crab Moves to Edge of the World

11 Running your code Place Multiple Crabs into the World Click the Act and Run Buttons

12 Running your code Crabs All Move to Edge of the World

13 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() { turn(5); } Replace move() with turn(5) and compile.

14 Turning an Actor Crab Spins to the Right

15 Turning an Actor Set turn to -5 and recompile

16 Turning an Actor Crab Spins to the Left

17 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.

18 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); }

19 Moving and Turning move and turn

20 Moving and Turning Crab moves in a circle

21 Errors Syntax Error - Expected Semicolon

22 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

23 Various Errors

24

25

26

27

28

29

30

31

32

33

34 Method Signatures void turn (int angle) boolean atWorldEdge ( ) void move ( ) Return Type Method Name Parameters 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

35 Documentation View Right Click on Animal Class and Click on Open editor

36 Documentation View Switch Between Source and Documentation Method Summary

37 Documentation View

38 Running A Method Right Click on the Crab and Select inherited from Animal Then Click on atWorldEdge ( )

39 Getting A Return Value

40 Running A Method Move the Crab to the Edge of the World and Repeat the Experiment

41 Getting A Return Value

42 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 ( ); }

43 Turning at-the-world-edge The Crab turns only if it is in contact with the world edge

44 A Logic Error Place the move () Inside the if Statement

45 A Logic Error If the Crab is at the Edge, Then It Turns Around. If the Crab is Not Near the Edge Nothing Happens.

46 Staying off Edges Crab should run around the world, peforming a complete turn once it hits a wall.

47 Reversing Direction Set the Turn to 180 and the Crab Should Reverse Direction

48 Reversing Direction Crab Moves from Side to Side

49 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 40 th step (Step 40, Step 80, Step 120 …).

50 Introducing Variables Does not help us in this situations

51 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

52 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

53 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 TypeName

54 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 TypeName Initial Value

55 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.

56 Introducing Variables The algorithm for turning around after every 40 th 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.

57 Introducing Variables Increase stepCount by 1 Check if we are taking a 40 th step Reset stepCount and turn around Take a step

58 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.

59 Concept Summary


Download ppt "Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It."

Similar presentations


Ads by Google