Chapter 2 The Little Crab Program Little Crab

  • Slides: 59
Download presentation
Chapter 2 – The Little Crab Program:

Chapter 2 – The Little Crab Program:

Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal is an

Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an 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 Click Run and observe what happens

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

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

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, Greenfoot. Image, and Greenfoot) /*

Moving an Actor import greenfoot. *; // (World, Actor, Greenfoot. Image, 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 } }

Moving an Actor import greenfoot. *; // (World, Actor, Greenfoot. Image, and Greenfoot) /*

Moving an Actor import greenfoot. *; // (World, Actor, Greenfoot. Image, 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(); 5 pixel in the current direction. } }

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

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

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 Crab Moves to Edge of the World

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

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

Running your code Crabs All Move to Edge of the World

Turning an Actor import greenfoot. *; // (World, Actor, Greenfoot. Image, and Greenfoot) /*

Turning an Actor import greenfoot. *; // (World, Actor, Greenfoot. Image, 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 Crab Spins to the Right

Turning an Actor Set turn to -5 and recompile

Turning an Actor Set turn to -5 and recompile

Turning an Actor Crab Spins to the Left

Turning an Actor Crab Spins to the Left

Turning an Actor The parameter given to the turn() method defines the change of

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, Greenfoot. Image, and Greenfoot) /*

Moving and Turning import greenfoot. *; // (World, Actor, Greenfoot. Image, 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 move and turn

Moving and Turning Crab moves in a circle

Moving and Turning Crab moves in a circle

Errors Syntax Error - Expected Semicolon

Errors Syntax Error - Expected Semicolon

Various Errors 1. ‘; ’ expected (Terminator symbol) 2. cannot find symbol - method

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

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 at. World.

Method Signatures Return Type Method Name Parameters void turn (int angle) boolean at. World. Edge ( ) 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 Right Click on Animal Class and Click on Open editor

Documentation View Method Summary Switch Between Source and Documentation

Documentation View Method Summary Switch Between Source and Documentation

Documentation View

Documentation View

Running A Method Right Click on the Crab and Select inherited from Animal Then

Running A Method Right Click on the Crab and Select inherited from Animal Then Click on at. World. Edge ( )

Getting A Return Value

Getting A Return Value

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

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

Getting A Return Value

Getting A Return Value

Turning at-the-world-edge import greenfoot. *; // (World, Actor, Greenfoot. Image, and Greenfoot) /* *

Turning at-the-world-edge import greenfoot. *; // (World, Actor, Greenfoot. Image, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { if ( at. World. Edge ( ) ) { turn (17); } move ( ); } }

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

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 Place the move () Inside the if Statement

A Logic Error If the Crab is at the Edge, Then It Turns Around.

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

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 Set the Turn to 180 and the Crab Should Reverse Direction

Reversing Direction Crab Moves from Side to Side

Reversing Direction Crab Moves from Side to Side

Introducing Variables In the previous example, we made the Crab turn around whenever it

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 …).

Introducing Variables Does not help us in this situations

Introducing Variables Does not help us in this situations

Introducing Variables We obviously need a way to count “steps” (a step is 1

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 step. Count = 0

Introducing Variables We obviously need a way to count steps. This requires that we

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 step. Count = 0 Type

Introducing Variables We obviously need a way to count steps. This requires that we

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 step. Count = 0 Type Name

Introducing Variables We obviously need a way to count steps. This requires that we

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 step. Count = 0 Type Name Initial Value

Introducing Variables Note the placement of the variables (outside of the move method). It

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 40 th step: 1. Increase

Introducing Variables The algorithm for turning around after every 40 th step: 1. Increase step. Count by 1. 2. If step. Count is equal to 40, then set step. Count to 0 and turn around. 3. Take a step.

Introducing Variables Increase step. Count by 1 Check if we are taking a 40

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

Summary of Programming Techniques In this chapter, we have seen how to call methods

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

Concept Summary