Presentation is loading. Please wait.

Presentation is loading. Please wait.

Improving the Crab Mrs. C. Furman August 19, 2010.

Similar presentations


Presentation on theme: "Improving the Crab Mrs. C. Furman August 19, 2010."— Presentation transcript:

1 Improving the Crab Mrs. C. Furman August 19, 2010

2 Adding Random Behavior  Currently, our crab moves from one edge to another in a straight line, turning in the same way each time.  Is this how crabs move???

3 int getRandomNumber (int limit)  We will be using this method to generate a random number. Return typeMethod NameFormal Parameter List

4 Greenfoot.getRandomNumber (20)  The above is a method call.  It returns a value from 0 – 19. That is 20 values to choose from. Class name method name Actual Parameter dot operator

5 Method Calls classname.method (parameter list)  This is the general form of a method call.  When we used move() or turn() we didn’t use a class name or object name, because move() and turn() are methods of our superclass.  If you are using methods of another class, you need to specify. or object

6 move() and turn()  To continue the general form of: object.methodname(parameters) object.methodname(parameters)  We can use the keyword this when we are calling methods in our own classes.  So, instead of: move() try this.move();

7 Exercise  Modify the act method in little-crab-1 by adding this and dot in front of each call to turn() and move().  Your act method should look as follows: public void act() public void act() { if ( this.atWorldEdge() ) if ( this.atWorldEdge() ) { this.turn(17); this.turn(17); } this.move(); this.move(); }

8 Random turn  Say we want to turn our crab occasionally on a random call to act. if (something is true) { this.turn(5); this.turn(5);}

9 What can we use to replace the something is true?  Say we want to turn 10% of the time.  So if we generate 100 random numbers, we want to call this.turn(5), when the numbers 0 – 9 are generated.

10 Relationship Operators  < less than  > greater than  <= less than or equal  >= greater than or equal  != not equal  == equal  note: to ask if two things are equal you need 2 = signs.

11 Boolean Expression  An expression that can be determined to be either true or false.  To find 10%, we can generate 100 numbers and if the number is less than 10 (0 – 9),then it would be in the range of the 10% of the 100 numbers. Greenfoot.getRandomNumber (100) < 10 Greenfoot.getRandomNumber (100) < 10

12 Modify your code public void act() { if(this.atWorldEdge()) if(this.atWorldEdge()) { this.turn(17); this.turn(17); } if (Greenfoot.getRandomNumber(100) < 10) if (Greenfoot.getRandomNumber(100) < 10) { this.turn(5); this.turn(5); } this.move(); this.move();}

13 Exercise Instead of turning 5 each time, modify your program to turn a random amount.

14 Exercise  Our crab is still only turning to the right. That’s not normal behavior for a crab, so let’s fix this. Modify your code so that the crab turns either left or right up to 45 degrees each time it turns.

15 Exercise  Run your project with multiple crabs. Do they all turn the same way at the same time?


Download ppt "Improving the Crab Mrs. C. Furman August 19, 2010."

Similar presentations


Ads by Google