This site requires JavaScript, please enable it in your browser!
Greenfoot back
Saveseals2
Saveseals2 wrote ...

2018/11/29

Need help please

Saveseals2 Saveseals2

2018/11/29

#
You are given the source code of the lookForWorm method that is currently implemented
 in the Crab class: public void lookForWorm() { if ( canSee(Worm.class) ) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 3) { Greenfoot.playSound("fanfare.wav"); // This method will be discussed later in
 question 3.. reportToughestLobster(); Greenfoot.stop(); } } } You can assume here that the canSee(Worm.class) method call returns true if the crab has encountered either a Worm object or an object of the FatWorm subclass, thanks to the inheritance relationship
 between such two classes. On this page below, rewrite the lookForWorm method so that the “wormsEaten” variable is increased by 2 every time the crab eats a FatWorm, by 1 every time it eats just a regular Worm. Everything else stays as it was. public void lookForWorm()
 { } 2. (10 points) Still for the little-crab-5 scenario. You can see that the objects are placed “by hand” in the CrabWorld, one after another. On the next page, use either while or for loops to rewrite the populateWorld method in order to generate and randomly
 place one Crab, three Lobsters, ten Worms, and five FatWorms in the CrabWorld. import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage import java.util.Random; public class CrabWorld extends World { /** * Create the crab world (the beach). Our
 world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); populateWorld(); } /** * Create the objects for the start of the game. */ public void populateWorld() { addObject(new Crab(), 300, 300); addObject(new
 Lobster(), 90, 70); addObject(new Lobster(), 390, 200); addObject(new Lobster(), 360, 500); addObject(new Worm(), 20, 500); addObject(new Worm(), 30, 200); addObject(new Worm(), 60, 90); addObject(new Worm(), 80, 310); addObject(new Worm(), 150, 50); addObject(new
 Worm(), 210, 410); addObject(new Worm(), 220, 520); addObject(new Worm(), 380, 330); addObject(new Worm(), 410, 270); addObject(new Worm(), 530, 30); addObject(new FatWorm(), 20, 40); addObject(new FatWorm(), 87, 223); addObject(new FatWorm(), 80, 360); addObject(new
 FatWorm(), 111, 222); addObject(new FatWorm(), 345, 23); } // This method will be discussed later in question 3… public Lobster toughestLobster(Lobster lobsters){ return null; } } public void populateWorld() { } 3. Still in the little-crab-5 scenario, the
 following is the source code for the lookForCrab method in the Lobster class: public void lookForCrab() { if ( canSee(Crab.class) ) { eat(Crab.class); Greenfoot.playSound("au.wav"); // Greenfoot.stop(); getWorld().addObject( new Crab(), Greenfoot.getRandomNumber(560),
 Greenfoot.getRandomNumber(560) ); setCrabsEaten(getCrabsEaten()+1); } } Notice that the Greenfoot.stop() statement has been replaced by an addObject statement such that a new Crab object is created and placed in the CrabWorld every time an old one is eaten
 by one of the lobsters. Also, we update the number of crabs that this lobster has eaten so far. Here you may assume a private variable crabsEaten and its “setter” and “getter” methods have been added into the Lobster class: private int crabsEaten = 0; … …
 public void setCrabsEaten (int crabs) { crabsEaten = crabs; } public int getCrabsEaten() { return crabsEaten; } Notice that in the end of CrabWorld, we added a new method called toughestLobster. This method takes an array of lobsters as its argument and is
 supposed to return the one who has eaten the most crabs. Your job is, on the next page, to replace the return null; statement with a piece of code that accomplishes its task. This toughestLobster method will eventually be called in the reportToughestLobster
 method of the Crab class such that when the crab has eaten three worms (could be a combination of regular and fat worms), the lobster who happens to eat the most number of crabs is labeled before the scenario stops. public Lobster toughestLobster(Lobster lobsters){
You need to login to post a reply.