greenfoot and the little crab

28
Greenfoot and the Greenfoot and the Little Crab Little Crab Mrs. Furman Mrs. Furman August 17, 2010 August 17, 2010

Upload: alphonse-meller

Post on 02-Jan-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Greenfoot and the Little Crab. Mrs. Furman August 17, 2010. Open Scenario. Open Greenfoot Click on Scenario and open the little-crab scenario in the chapter 02 folder Click Compile All. Superclasses and Subclasses. What World class do you see? What are the subclasses of Actor? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Greenfoot and the Little Crab

Greenfoot and the Greenfoot and the Little CrabLittle CrabMrs. FurmanMrs. Furman

August 17, 2010August 17, 2010

Page 2: Greenfoot and the Little Crab

Open ScenarioOpen Scenario Open GreenfootOpen Greenfoot Click on Scenario and open the little-crab Click on Scenario and open the little-crab

scenario in the chapter 02 folderscenario in the chapter 02 folder Click Compile AllClick Compile All

Page 3: Greenfoot and the Little Crab

Superclasses and Superclasses and SubclassesSubclasses

What World class do What World class do you see?you see?

What are the What are the subclasses of Actor?subclasses of Actor?

Which Actor class are Which Actor class are we allowed to create we allowed to create objects of?objects of?

Page 4: Greenfoot and the Little Crab

Is-a relationshipIs-a relationship

Remember in the hierarchy:Remember in the hierarchy: Crab is-a AnimalCrab is-a Animal Animal is-a ActorAnimal is-a Actor So… Crab is-a ActorSo… Crab is-a Actor

Page 5: Greenfoot and the Little Crab

Open Crab Source CodeOpen Crab Source Code

Place a crab in the World and run the Place a crab in the World and run the program. What happens?program. What happens?

Try to open the Crab source code. Try to open the Crab source code. What happens?What happens?

Page 6: Greenfoot and the Little Crab

Making Crab MoveMaking Crab Move

We can make Crab move by invoking We can make Crab move by invoking (calling) methods in Crab’s act method.(calling) methods in Crab’s act method.

Page 7: Greenfoot and the Little Crab

Making Crab move Cont.Making Crab move Cont. The act method looks like:The act method looks like:

public void act()public void act()

{{

//Add your action here//Add your action here

}} public is an public is an access specifieraccess specifier it tells the compiler it tells the compiler

who can call the method. Any class using this who can call the method. Any class using this class can invoke and use the act method.class can invoke and use the act method.

Page 8: Greenfoot and the Little Crab

Making Crab Move Cont.Making Crab Move Cont. The act method looks like:The act method looks like:

public void act()public void act()

{{

//Add your action here//Add your action here

}} After public is the return type, void and then the After public is the return type, void and then the

method name, and parameter list.method name, and parameter list. Remember this is called the method signature.Remember this is called the method signature.

Page 9: Greenfoot and the Little Crab

Making Crab Move Cont.Making Crab Move Cont. The act method looks like:The act method looks like:public void act()public void act(){{ //Add your action here//Add your action here}} The braces indicate the start and end of the The braces indicate the start and end of the

method. In between the braces, we write the method. In between the braces, we write the method method bodybody. The body is where we add our . The body is where we add our code.code.

Page 10: Greenfoot and the Little Crab

Making Crab Move Cont.Making Crab Move Cont. The act method looks like:The act method looks like:public void act()public void act(){{ //Add your action here//Add your action here}} //Add your action here is a comment. Any line //Add your action here is a comment. Any line

that starts with // or /* is a comment line. that starts with // or /* is a comment line. These are ignored by the computer. These are ignored by the computer.

Replace the comment with the line: move();Replace the comment with the line: move();

Page 11: Greenfoot and the Little Crab

Making Crab Move Cont.Making Crab Move Cont. The act method looks like:The act method looks like:public void act()public void act(){{ move();move();}} By placing move(); in the body we are By placing move(); in the body we are invokinginvoking

the method move();the method move(); This is a This is a statementstatement- all statements end in a - all statements end in a

semicolon.semicolon.

Page 12: Greenfoot and the Little Crab

Compile and Add CrabCompile and Add Crab

Click Compile All and add a Crab to your Click Compile All and add a Crab to your World. World.

Click the Act and Run buttonsClick the Act and Run buttons What happens?What happens? Place multiple Crabs in the World, what Place multiple Crabs in the World, what

happens?happens?

Page 13: Greenfoot and the Little Crab

MethodsMethods The instruction move() is called a The instruction move() is called a method call.method call. A A methodmethod is an action that an object knows is an action that an object knows

how to do (here, the object is the crab) and a how to do (here, the object is the crab) and a method callmethod call is an instruction telling the crab to is an instruction telling the crab to do it. do it.

The parentheses are part of the method call. The parentheses are part of the method call. Instructions like this end with a semicolon.Instructions like this end with a semicolon.

Page 14: Greenfoot and the Little Crab

Try the turn methodTry the turn method The act method looks like:The act method looks like:public void act()public void act(){{ turn(5);turn(5);}} Change the move() call to a call to the Change the move() call to a call to the

turn method. Type turn(5);turn method. Type turn(5); Compile and try the Act and Run buttons.Compile and try the Act and Run buttons.

Page 15: Greenfoot and the Little Crab

Trying other turnsTrying other turns The value we sent in the call turn(5); is The value we sent in the call turn(5); is

called an called an actual parameter.actual parameter. Parameters are values that are sent to Parameters are values that are sent to

methods. methods. We can send other values too. Change We can send other values too. Change

the turn(5) method to turn a different the turn(5) method to turn a different amount. amount.

Compile and run, what happens?Compile and run, what happens?

Page 16: Greenfoot and the Little Crab

Turning Cont.Turning Cont.

What happens when you put a negative What happens when you put a negative number in the turn method?number in the turn method?

Try it and see.Try it and see.

Page 17: Greenfoot and the Little Crab

Moving and TurningMoving and Turning Lets try to turn and move. Lets try to turn and move. The act method looks like:The act method looks like:public void act()public void act(){{ move();move(); turn (5);turn (5);}} Change the act method, re-compile and run. Change the act method, re-compile and run. What happens?What happens?

Page 18: Greenfoot and the Little Crab

ErrorsErrors

Remove the semicolon at the end of the Remove the semicolon at the end of the move() statement. Re-compile, what move() statement. Re-compile, what happens?happens?

Try changing move(); to Move(); Try changing move(); to Move(); Recompile, what happens? Why?Recompile, what happens? Why?

Page 19: Greenfoot and the Little Crab

Actors falling off cliffsActors falling off cliffs

In Greenfoot, the Actor can not leave the In Greenfoot, the Actor can not leave the World. World.

That is why we are stopped at the edge That is why we are stopped at the edge of the World. of the World.

Let’s get Crab to know that it is at the Let’s get Crab to know that it is at the edge.edge.

Page 20: Greenfoot and the Little Crab

Methods to find the edgeMethods to find the edge move() and turn() are methods located in move() and turn() are methods located in

the Animal Class.the Animal Class. Double click on Animal and you will get Double click on Animal and you will get

the documentation of this class. the documentation of this class. Why look at Animal? Crab is-a AnimalWhy look at Animal? Crab is-a Animal Since we do not see any other methods Since we do not see any other methods

in Crab, they must be in the superclasses in Crab, they must be in the superclasses that Crab that Crab inheritsinherits from. from.

Page 21: Greenfoot and the Little Crab

DocumentationDocumentation

Change from Source Code to Documentation

Page 22: Greenfoot and the Little Crab

DocumentationDocumentation

Sounds like the one wewant!!

Page 23: Greenfoot and the Little Crab

boolean atWorldEdge()boolean atWorldEdge()

What’s the return type?What’s the return type? What does that mean?What does that mean? What parameters does it take?What parameters does it take?

Page 24: Greenfoot and the Little Crab

ExerciseExercise Create a Crab object and place it in the Create a Crab object and place it in the

WorldWorld Invoke the atWorldEdge() method. What Invoke the atWorldEdge() method. What

happens?happens? Let the Crab run to the edge of the Let the Crab run to the edge of the

screen and call atWorldEdge() again. screen and call atWorldEdge() again. What happens?What happens?

Page 25: Greenfoot and the Little Crab

if statementif statement

There are times when we want our There are times when we want our objects to do specific things based on objects to do specific things based on different situations. For example, if they different situations. For example, if they are at the edge of the world, what could are at the edge of the world, what could we do?we do?

Page 26: Greenfoot and the Little Crab

if statementif statement When we have conditions for when we When we have conditions for when we

complete instructions we need to use if complete instructions we need to use if statements.statements.

The basic structure is as follows:The basic structure is as follows: if (condition)if (condition) {{ statementsstatements }}The condition has to return a boolean value.The condition has to return a boolean value.

Page 27: Greenfoot and the Little Crab

Modify act()Modify act() Change the act method so that it uses the following if Change the act method so that it uses the following if

statementstatementpublic void act()public void act(){{ if (atWorldEdge() )if (atWorldEdge() ) { { turn(17);turn(17); }} move();move();}} Re-compile and run. What happens?Re-compile and run. What happens?

Page 28: Greenfoot and the Little Crab

ExerciseExercise

Experiment with different values in the Experiment with different values in the turn method. Find a value that really turn method. Find a value that really works well. works well.

Place the move() statement inside the if-Place the move() statement inside the if-statement as well. What happens? Can statement as well. What happens? Can you explain why?you explain why?