Presentation is loading. Please wait.

Presentation is loading. Please wait.

karel_part4_functions_2

Similar presentations


Presentation on theme: "karel_part4_functions_2"— Presentation transcript:

1 karel_part4_functions_2
Day 2 karel_part4_functions_2

2 karel_part4_functions_2
Functions return values or Objects. Using a function allows the programmer to focus on other task. Using a function allows the programmer to leave the details till later or someone else. karel_part4_functions_2

3 karel_part4_functions_2
A Karel Example This method has NO parameters and returns true if there are two or more beepers on the current corner without changing the world. public boolean twoOrMoreBeepersOnCorner() { if (nextToABeeper() ) { pickBeeper(); if ( nextToABeeper() ) { putBeeper(); return true; } return false; karel_part4_functions_2

4 karel_part4_functions_2
A Karel Example How is the method to return true only if there are EXACTLY two beepers on this corner? public boolean exactlyTwoBeepersOnCorner() { if (nextToABeeper() ) { pickBeeper(); if ( nextToABeeper() ) { putBeeper(); // This code from the return true; //the previous function //changes to what you ask? } putBeeper(); return false; karel_part4_functions_2

5 karel_part4_functions_2
To this I say. pickBeeper() if (nextToABeeper() ) { putBeeper(); return false; } else // not next to a beeper { return true; karel_part4_functions_2

6 Here’s the complete function
public boolean exactlyTwoBeepersOnCorner() { if (nextToABeeper() ) pickBeeper(); if ( nextToABeeper() ) pickBeeper() putBeeper(); return false; //next to a third beeper if it gets here } else return true; //not next to a third beeper, but exactly two if it //gets here putBeeper(); //next to only one beeper if it gets here return false; //not next to any beepers or only one if it gets here karel_part4_functions_2

7 karel_part4_functions_2
A Question for You Write a method that returns true only if there is one or two Beepers on the corner! public boolean oneOrTwoBeepersOnCorner() { return ???????; } karel_part4_functions_2

8 karel_part4_functions_2
A Question for You Write a method that returns true only if there is one or two Beepers on the corner! public boolean oneOrTwoBeepersOnCorner() { return nextToABeeper() || exactlyTwoBeepersOnCorner(); } karel_part4_functions_2

9 karel_part4_functions_2
A Question for You Does the method work as intended? Why does it fail? isNextToABeeper() returns true if next to one, two, three, …. Beepers How do we fix it? Replace isNextToABeeper() with the method: nextToOneBeeper() Which we need to write. karel_part4_functions_2

10 karel_part4_functions_2
A Question for You This method has NO parameters and returns true only if there is exactly one or two beepers on the corner. public boolean oneOrTwoBeepersOnCorner() { return nextToOneBeeper() || exactlyTwoBeepersOnCorner(); } karel_part4_functions_2

11 karel_part4_functions_2
A Question for You public boolean nextToOneBeeper() { if ( nextToABeeper() ) { pickBeeper() putBeeper(); return false; } else { return true; return false karel_part4_functions_2

12 karel_part4_functions_2
A Karel Example This method has NO parameters and returns true only if there is a wall to the left of the Robot. public boolean wallOnLeft() { turnLeft(); if ( isFrontClear() ) { turnRight(); return false; } return true; karel_part4_functions_2

13 karel_part4_functions_2
You try one Implement the method numLeftTurns2FaceNorth which returns the number of times a Robot must turn left to face north. public int numLeftTurns2FaceNorth() The Robot must be facing the original direction upon completion of the method(function)! karel_part4_functions_2

14 karel_part4_functions_2
Your Assignment Implementing all methods declared in FunctionRobot class. You can invoke MainDriver1 to test you function implementation. This time, ignore the World and view the terminal. Messages will be displayed indicating the status of each function, and a final statement summarizing the entire class. Upon completion, invoke MainDriver2 and be amazed! See handout (Karel_part4_function.doc) for details. karel_part4_functions_2


Download ppt "karel_part4_functions_2"

Similar presentations


Ads by Google