50 likes | 159 Vues
Explore how to program a Lobster subclass, ensuring it can behave in a World scenario by eating Crabs. Develop specific methods like lookForCrab(), turnAtEdge(), randomTurn(), and act() for Lobsters. Test the program with Crabs, Lobsters, and Worms to see if the Crab can outsmart the Lobster.
E N D
Adding Lobsters Mrs. C. Furman August 23, 2010
Exercise • Add a new class to your scenario. • A Lobster would be a subclass of what class? • Create a Lobster class and give it the prepared Lobster image. • What will the Lobster do when it is placed in the World?
Program Lobster to Eat Crab • Add a method to lookForCrab() that will look for a crab and eat it. This method will be similar to crab’s lookForWorm() method. • Add a method to turnAtEdge(), this should be identical to crabs, so you can copy and paste it.
Lobster Programming Cont. • Add a randomTurn() method that is like crab. Copy and paste it • Add a act method that calls lookForCrab, turnAtEdge, randomTurn and move.
Exercise • Run the program and add a crab, 3 lobsters and many worms into the World. • Is the crab able to eat all the worms before it is eaten by a Lobster?