Adding and eating worms
1 / 22

Adding and Eating Worms - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Adding and Eating Worms' - marie

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Adding and eating worms

Adding and Eating Worms

Mrs. C. Furman

August 23, 2010

Let s add a worm class
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
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.

Name and add a picture
Name and add a picture

  • After you name the class

  • Add a picture. Choose the worm.png

  • Select ok


  • 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?

How can we get crabs to eat worms
How can we get Crabs to Eat Worms?

  • What would we need to be able to do to get Crabs to eat Worms?

Making crab eat worms
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???

Boolean cansee java lang class clss
boolean canSee(java.lang.Class clss)

  • What is the return type?

  • What is the method name?

  • What is the parameter?

Void eat java lang class clss
void eat (java.lang.Class clss)

  • What is the return type?

  • What is the method name?

  • What is the parameter?

Using cansee and eat to eat worms
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

The code
The code

if (this.canSee(Worm.class))



  • Worm.class – this specifies the class we are looking for and eating.

  • Add the above if statement to the act method.

Adding methods
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.

Lookforworm method
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.


  • When we add a new Method, we will want to add a description of what the method does.

  • What does lookForWorms do?

Different types of comments
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 */



* 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.


Adding the method
Adding the method

  • Is our method an action or a question?

  • What should our return type be?

Add the method
Add the method

public void lookForWorm()


if (this.canSee(Worm.class) )




Modify act method
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.

Other methods
Other Methods

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


  • 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();


  • 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();