140 likes | 148 Views
Learn how to use functions in Karel programming to focus on other tasks, postpone details, and return values or objects. Examples include counting beepers and detecting walls.
E N D
Functions Day 2 karel_part4_functions_2
Functions • 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
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; } putBeeper(); } return false; } 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
To this I say. pickBeeper() if (nextToABeeper() ) { putBeeper(); putBeeper(); return false; } else // not next to a beeper { putBeeper(); putBeeper(); return true; } karel_part4_functions_2
Here’s the complete function public boolean exactlyTwoBeepersOnCorner() { if (nextToABeeper() ) { pickBeeper(); if ( nextToABeeper() ) { pickBeeper() if (nextToABeeper() ) { putBeeper(); putBeeper(); return false; //next to a third beeper if it gets here } else { putBeeper(); putBeeper(); 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
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
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
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
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
A Question for You public boolean nextToOneBeeper() { if ( nextToABeeper() ) { pickBeeper() if ( nextToABeeper() ) { putBeeper(); return false; } else { putBeeper(); return true; } } return false } 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; } turnRight(); return true; } 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
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