1 / 22

Adding and Eating Worms

Adding and Eating Worms. Mrs. C. Furman August 23, 2010. Let’s add a Worm Class. What class should we extend to add a Worm? Worm is-a Animal Select New Subclass from the Animal menu. Click here. Setting up the new Subclass. Enter the name as Worm

marie
Télécharger la présentation

Adding and Eating Worms

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. Adding and Eating Worms Mrs. C. Furman August 23, 2010

  2. Let’s add a Worm Class • What class should we extend to add a Worm? • Worm is-a Animal • Select New Subclass from the Animal menu Click here

  3. Setting up the new Subclass • Enter the name as Worm • Class names in Java should always start with a capital letter • Rules for class names: • Start with a capital letter • Contain only letters, numbers or underscores • Can’t be a keyword • As a convention it should describe the class.

  4. Name and add a picture • After you name the class • Add a picture. Choose the worm.png • Select ok

  5. Exercise • Compile • Add some worms and crabs to the world. • Run the scenario • What happens to the worms? • What happens when a crab meets a worm?

  6. How can we get Crabs to Eat Worms? • What would we need to be able to do to get Crabs to eat Worms?

  7. Making Crab Eat Worms • Lets see if there are any classes in Animal that can help us… • Open the source code, and choose documentation. • Any methods???

  8. boolean canSee(java.lang.Class clss) • What is the return type? • What is the method name? • What is the parameter?

  9. void eat (java.lang.Class clss) • What is the return type? • What is the method name? • What is the parameter?

  10. Using canSee and eat to eat worms. • canSee - will tell us is there is a worm near us. • it returns true or false • eat – eats the worm

  11. The code if (this.canSee(Worm.class)) { this.eat(Worm.class); } • Worm.class – this specifies the class we are looking for and eating. • Add the above if statement to the act method.

  12. Adding Methods • Our methods should specify a single “behavior” that we want to perform. • act is starting to have too many behaviors that it is charged with.

  13. lookForWorm method • We would like to create a lookForWorm method that contains the if statement we just added to act. • It will look for worms and then eat them.

  14. Comments • When we add a new Method, we will want to add a description of what the method does. • What does lookForWorms do?

  15. Different Types of Comments • // - used to generate a single line of code • /* - starts a multiple line of comment and ends with */ • /** - starts a javadoc comment. This will generate the documentation we see when we open the code. It also ends with */

  16. Comment /** * Check whether we have found a worm * If we have found a worm, we eat it. * If we haven’t, we do nothing. * PostCondition: After this method has * been executed worms near the crab * have been removed. */

  17. Adding the method • Is our method an action or a question? • What should our return type be?

  18. Add the method public void lookForWorm() { if (this.canSee(Worm.class) ) { this.eat(Worm.class); } }

  19. Modify Act method • Remove the eating if statement from act • Replace with a call to the lookForWorm method • this.lookForWorm(); • Compile and Run the code.

  20. Other Methods • Are there other behaviors in Act that can be pulled out into a method?

  21. Exercise • Create method randomTurn. • What would the return type be? Is it a question, or an action? • Move the code that does the random turning out of Act and put it into randomTurn. • Replace the code in Act with a call to this.randomTurn();

  22. Exercise • Create another method for turning at the edge called turnAtEdge • What is the return type? Is it a question, or an action? • Remove the code from Act and move it into turnAtEdge. Replace the code in Act with a call to this.turnAtEdge();

More Related