220 likes | 328 Views
This guide focuses on extending animal classes in Java by adding a new subclass for worms. It covers creating a 'Worm' class, implementing methods for crabs to eat worms, and structuring the code effectively. The document outlines rules for naming classes, adding comments, and creating specific behaviors, such as 'lookForWorm' and 'randomTurn' methods. You'll also learn how to modify existing methods to incorporate new behaviors and ensure that crabs interact appropriately with worms in your Java scenario.
E N D
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 • 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 • After you name the class • Add a picture. Choose the worm.png • Select ok
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?
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 • 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) • What is the return type? • What is the method name? • What is the parameter?
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. • canSee - will tell us is there is a worm near us. • it returns true or false • eat – eats the worm
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.
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 • 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.
Comments • 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 • // - 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 */
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. */
Adding the method • Is our method an action or a question? • What should our return type be?
Add the method public void lookForWorm() { if (this.canSee(Worm.class) ) { this.eat(Worm.class); } }
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 • Are there other behaviors in Act that can be pulled out into a method?
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();
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();