1 / 28

Greenfoot and the Little Crab

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?

Download Presentation

Greenfoot and the Little Crab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Open Scenario • Open Greenfoot • Click on Scenario and open the little-crab scenario in the chapter 02 folder • Click Compile All

  3. Superclasses and Subclasses • What World class do you see? • What are the subclasses of Actor? • Which Actor class are we allowed to create objects of?

  4. Is-a relationship • Remember in the hierarchy: • Crab is-a Animal • Animal is-a Actor • So… Crab is-a Actor

  5. Open Crab Source Code • Place a crab in the World and run the program. What happens? • Try to open the Crab source code. • What happens?

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

  7. Making Crab move Cont. • The act method looks like: public void act() { //Add your action here } • public is an access specifier it tells the compiler who can call the method. Any class using this class can invoke and use the act method.

  8. Making Crab Move Cont. • The act method looks like: public void act() { //Add your action here } • After public is the return type, void and then the method name, and parameter list. • Remember this is called the method signature.

  9. Making Crab Move Cont. • The act method looks like: public void act() { //Add your action here } • The braces indicate the start and end of the method. In between the braces, we write the method body. The body is where we add our code.

  10. Making Crab Move Cont. • The act method looks like: public void act() { //Add your action here } • //Add your action here is a comment. Any line that starts with // or /* is a comment line. These are ignored by the computer. • Replace the comment with the line: move();

  11. Making Crab Move Cont. • The act method looks like: public void act() { move(); } • By placing move(); in the body we are invoking the method move(); • This is a statement- all statements end in a semicolon.

  12. Compile and Add Crab • Click Compile All and add a Crab to your World. • Click the Act and Run buttons • What happens? • Place multiple Crabs in the World, what happens?

  13. Methods • The instruction move() is called a method call. • A method is an action that an object knows how to do (here, the object is the crab) and a method call is an instruction telling the crab to do it. • The parentheses are part of the method call. • Instructions like this end with a semicolon.

  14. Try the turn method • The act method looks like: public void act() { turn(5); } • Change the move() call to a call to the turn method. Type turn(5); • Compile and try the Act and Run buttons.

  15. Trying other turns • The value we sent in the call turn(5); is called an actual parameter. • Parameters are values that are sent to methods. • We can send other values too. Change the turn(5) method to turn a different amount. • Compile and run, what happens?

  16. Turning Cont. • What happens when you put a negative number in the turn method? • Try it and see.

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

  18. Errors • Remove the semicolon at the end of the move() statement. Re-compile, what happens? • Try changing move(); to Move(); Recompile, what happens? Why?

  19. Actors falling off cliffs • In Greenfoot, the Actor can not leave the World. • That is why we are stopped at the edge of the World. • Let’s get Crab to know that it is at the edge.

  20. Methods to find the edge • move() and turn() are methods located in the Animal Class. • Double click on Animal and you will get the documentation of this class. • Why look at Animal? Crab is-a Animal • Since we do not see any other methods in Crab, they must be in the superclasses that Crab inherits from.

  21. Documentation Change from Source Code to Documentation

  22. Documentation Sounds like the one we want!!

  23. boolean atWorldEdge() • What’s the return type? • What does that mean? • What parameters does it take?

  24. Exercise • Create a Crab object and place it in the World • Invoke the atWorldEdge() method. What happens? • Let the Crab run to the edge of the screen and call atWorldEdge() again. What happens?

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

  26. if statement • When we have conditions for when we complete instructions we need to use if statements. • The basic structure is as follows: if (condition) { statements } The condition has to return a boolean value.

  27. Modify act() • Change the act method so that it uses the following if statement public void act() { if (atWorldEdge() ) { turn(17); } move(); } • Re-compile and run. What happens?

  28. Exercise • Experiment with different values in the turn method. Find a value that really works well. • Place the move() statement inside the if-statement as well. What happens? Can you explain why?

More Related