![]() Both methods require parameter that is a class i.e.First method checks if crab sees worm – bumps into worm.In Documentation view in the Animal class we see two methods: boolean canSee( clss) Returns true if we can see an object of class 'clss' right where we are void eat( clss) Try to eat an object of class 'clss'.What do you observe? What do the worms do? What happens when a crab meets a worm?Įating Worms From Introduction to Programming with Greenfoot by Michael Kölling Used with permission From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 3.7: Add some worms to your 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Īdding 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Īdding New Actor (Worm) cont. Exercise 3.6: Try running the scenario with multiple crabs in the world.Exercise 3.5: Modify code so crab turns either right or left by up to 45 degrees.Exercise 3.4: Replace the turn(5) with random turn 45 between 0 and 44.Exercise 3.3: Experiment with different probabilities for turning.2 = 2 result is true != not equal to i.e. 2 > 33 result is false =greater than or equal to (not less than)i.e. Results in true or false greater than i.e.Relational Operators From Introduction to Programming with Greenfoot by Michael Kölling Used with permission getRandomNumber belongs to class Greenfoot.static int getRandomNumber(int limit).Methods belonging to classes have static in their method signature (definition).Methods Belong to Objects or Classes From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Parameter 20 will randomly generate a number from 0 to 19 (one less than the parameter value).Greenfoot.getRandomNumber(20) – in Greenfoot class. ![]() Add random number method to vary the crab’s walk. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |