1 / 24

Greenfoot

Greenfoot. Chapter 3 Improving the Crab – More Sophisticated Programming. Adding Random Behavior. Dot notation – specify the class or object for method not in our class or inherited followed by a period (dot) . before the method name Add random number method to vary the crab’s walk

khuyen
Télécharger la présentation

Greenfoot

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 Chapter 3Improving the Crab – More Sophisticated Programming From Introduction to Programming with Greenfoot by Michael Kölling Used with permission

  2. Adding Random Behavior From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Dot notation – specify the class or object for method not in our class or inherited followed by a period (dot) . before the method name • Add random number method to vary the crab’s walk • Greenfoot.getRandomNumber(20) – in Greenfoot class • Parameter 20 will randomly generate a number from 0 to 19 (one less than the parameter value)

  3. Methods Belong to Objects or Classes From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Invoking a method either • class-name.method-name (parameters); • object.method-name(parameters); • Methods belonging to classes have static in their method signature (definition) • static int getRandomNumber(int limit); • getRandomNumber belongs to class Greenfoot

  4. Relational Operators From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Operators to compare two values • Results in true or false < less than i.e. 2 < 33 result is true > greater than i.e. 2 > 33 result is false <= less than or equal to (not greater than)i.e. 2 <= 33 result is true >=greater than or equal to (not less than)i.e. 2 >= 33 result is false == equal to i.e. 2 == 2 result is true != not equal to i.e. 2 != 2 result is false

  5. Random Crab Walk • Exercise 3.3: Experiment with different probabilities for turning. • Exercise 3.4: Replace the turn(5) with random turn 45 between 0 and 44. • Exercise 3.5: Modify code so crab turns either right or left by up to 45 degrees. • Exercise 3.6: Try running the scenario with multiple crabs in the world. Do they all turn at the same time or independently? Why? From Introduction to Programming with Greenfoot by Michael Kölling Used with permission

  6. Adding New Actor (Worm) From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Worm is an animal (is a relationship) Class names in Java should always start with a capital letter

  7. Adding New Actor (Worm) cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 3.7: Add some worms to your world. Also add some crabs. Run the scenario. What do you observe? What do the worms do? What happens when a crab meets a worm?

  8. Eating Worms From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • In Documentation view in the Animal class we see two methods: boolean canSee(java.lang.Class clss) Returns true if we can see an object of class 'clss' right where we are void eat(java.lang.Class clss) Try to eat an object of class 'clss' • First method checks if crab sees worm – bumps into worm • Second method crab eats worm • Both methods require parameter that is a class i.e. Worm

  9. Eating Worms cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Use ifstatement to determine if crab sees worm – condition true then crab eats worm if ( canSee(Worm.class) ) { eat(Worm.class); }

  10. Creating New Methods From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Adding new behavior to the crab will result in an excessively long and complex act method Decomposition is the process of breaking a problem into smaller more manageable and related units (methods) Methods canSee and eat are logically related thus we will create 1 method (lookForWorm) to incorporate both methods Naming convention: Methods begin with lowercase - first letter of each word is uppercase – no spaces or other special symbols

  11. Creating New Methods cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Write comments (enclosed in /* and */) in methods to explain the coding for human maintenance – ignored by computer /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. */ public void lookForWorm() { if ( canSee(Worm.class) ) { eat(Worm.class); } }

  12. Calling the lookForWorm method From Introduction to Programming with Greenfoot by Michael Kölling Used with permission public void act() { if ( atWorldEdge() ) { turn(17); } if ( Greenfoot.getRandomNumber(100) < 10 ) { turn(Greenfoot.getRandomNumber (90)-45); } move(); lookForWorm(); }

  13. Creating New Methods cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 3.8: Create a new method named randomTurn – move the coding from the act method to the randomTurn method. Be sure to write appropriate comments. Exercise 3.9: Create a method named turnAtEdge – move the coding from the act method to the turnAtEdge method. Be sure to write appropriate comments.

  14. Calling your New Methods From Introduction to Programming with Greenfoot by Michael Kölling Used with permission New act method: public void act() { turnAtEdge(); randomTurn(); move(); lookForWorm(); }

  15. Adding Lobster Actor From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Lobsters like to eat crabs Exercise 3.10: Add new class (Lobster) to your scenario. Exercise 3.13: Copy the act method from the Crab class to the Lobster class replacing all references to Worm with Crab Exercise 3.14 Place a crab, three lobsters and many worms into the world. Does the crab manage to eat all the worms before it is caught by a lobster.

  16. Keyboard Control From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Greenfoot has method to check if a key on the keyboard has been pressed • Signature is: static boolean isKeyDown(String key) • String is text which is enclosed in quotes “ • Every key on the keyboard has a name • “A” for the A-key • “left” for the left cursor key (arrow) • Method returns boolean thus it can be used in an ifstatement

  17. Keyboard Control cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Greenfoot automatically saves classes and scenarios when their windows are closed. To keep a copy use Save a Copy As from the Scenario menu. Exercise 3.19: Remove the Random Turning and the Turn at Edge coding. Create another method called checkKeyPress which is called from the act method. This method makes the crab turn left (-4 degrees) or right (4 degrees) depending on the key that is pressed.

  18. Ending the Game From Introduction to Programming with Greenfoot by Michael Kölling Used with permission • Game ends when the Crab is caught by the Lobster • The API (Application Programmer’s Interface) Documentation lists all classes and methods available in Greenfoot. • Look at Greenfoot Class Documentation fromthe Help menu • Greenfoot has 6 classes (Actor, Greenfoot, GreenfootImage, GreenfootSound, MouseInfo and World)

  19. Ending the Game cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Methods in GreenFoot Exercise 3.22: Open Greenfoot API in your browser. Find a method thatstops the executionof a running scenario. Add code to end the game when a lobster eats a crab.

  20. Adding Sound From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Greenfoot Class Documentation has a method called playSound Crab scenario has two sound files: slurp.wav and au.wav Exercise 3.24: Add the“slurp.wav” sound whenthe Crab eats a Wormand the “au.wav” soundwhen a Lobster eatsa Crab

  21. Adding Sound cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Alternatively you can record your own sounds using a microphone Save the file as a WAV, AIFF or AU format Use your recorded sounds in Exercise 3.24

  22. Summary of Programming Techniques From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Used ifstatement for turning at random times and reacting to key presses Called methods from Greenfoot class (i.e. getRandomNumber, isKeyDown and playSound) by using the dot notation and the class name Called methods located in the class itself (i.e. local methods) Called methods that were defined in the superclass (i.e. inherited methods) Called static methods from other classes Explored how to read the API documentation of a class

  23. Concept Summary From Introduction to Programming with Greenfoot by Michael Kölling Used with permission When a method we wish to call is not in our class or inherited, we need to specify the class or object that has the method before the method name, followed by a dot. This is called dot notation. Methods that belong to classes (as opposed to objects) are marked with the keyword static in their signature. The are also call classmethods. A method definition defines a new action for objects of this class. The action is not immediately executed, but the method can be called with a method call later to execute it.

  24. Concept Summary cont. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Comments are written into the source code as explanations for human readers. They are ignored by the computer. The API Documentation lists all classes and methods available in Greenfoot. We often need to look up methods here.

More Related