Chapter 2 The Little Crab Program Little Crab



























































- Slides: 59

Chapter 2 – The Little Crab Program:

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 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, 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) /* * 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

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, 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 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, 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 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 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 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 at. World. Edge ( )

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, 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 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 40 th 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 step. Count = 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 step. Count = 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 step. Count = 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 step. Count = 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 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 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 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
Greenfoot little crab
1 little 2 little 3 little indian
1 little 2 little 3 little indian
Hát kết hợp bộ gõ cơ thể
Ng-html
Bổ thể
Tỉ lệ cơ thể trẻ em
Chó sói
Chụp tư thế worms-breton
Chúa sống lại
Các môn thể thao bắt đầu bằng tiếng chạy
Thế nào là hệ số cao nhất
Các châu lục và đại dương trên thế giới
Công của trọng lực
Trời xanh đây là của chúng ta thể thơ
Cách giải mật thư tọa độ
101012 bằng
độ dài liên kết
Các châu lục và đại dương trên thế giới
Thể thơ truyền thống
Quá trình desamine hóa có thể tạo ra
Một số thể thơ truyền thống
Cái miệng nó xinh thế chỉ nói điều hay thôi
Vẽ hình chiếu vuông góc của vật thể sau
Nguyên nhân của sự mỏi cơ sinh 8
đặc điểm cơ thể của người tối cổ
Ví dụ về giọng cùng tên
Vẽ hình chiếu đứng bằng cạnh của vật thể
Fecboak
Thẻ vin
đại từ thay thế
điện thế nghỉ
Tư thế ngồi viết
Diễn thế sinh thái là
Các loại đột biến cấu trúc nhiễm sắc thể
Thế nào là số nguyên tố
Tư thế ngồi viết
Lời thề hippocrates
Thiếu nhi thế giới liên hoan
ưu thế lai là gì
Sự nuôi và dạy con của hươu
Sự nuôi và dạy con của hổ
Sơ đồ cơ thể người
Từ ngữ thể hiện lòng nhân hậu
Thế nào là mạng điện lắp đặt kiểu nổi
Horseshoe crab evolutionary tree
Smoldering lymphoma
Pribilof golden crab
Crab criteria multiple myeloma
Crayfish phylum and class
Barnacle phylogeny
What is a female crab called
Atlantic wolffish pair
Jericho beach crabbing
Crab cladogram
Organism examples
Symbiotic relationship between honeyguide bird and badger
Crab
Shrimp phylum
A house for hermit crab summary