1 / 14

Functions

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.

jamesgillis
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Day 2 karel_part4_functions_2

  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

  3. 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

  4. 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. 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

  6. 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

  7. 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. 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. 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. 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. 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

  12. 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

  13. 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. 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

More Related