Functions Day 2 karelpart 4functions2 1 Functions Functions

  • Slides: 14
Download presentation
Functions Day 2 karel_part 4_functions_2 1

Functions Day 2 karel_part 4_functions_2 1

Functions • Functions return values or Objects. – Using a function allows the programmer

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_part 4_functions_2 2

A Karel Example • This method has NO parameters and returns true if there

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 two. Or. More. Beepers. On. Corner() { if (next. To. ABeeper() ) { pick. Beeper(); if ( next. To. ABeeper() ) { put. Beeper(); return true; } put. Beeper(); } return false; } karel_part 4_functions_2 3

A Karel Example • How is the method to return true only if there

A Karel Example • How is the method to return true only if there are EXACTLY two beepers on this corner? public boolean exactly. Two. Beepers. On. Corner() { if (next. To. ABeeper() ) { pick. Beeper(); if ( next. To. ABeeper() ) { put. Beeper(); // This code from the return true; //the previous function //changes to what you ask? } put. Beeper(); } return false; } karel_part 4_functions_2 4

To this I say. pick. Beeper() if (next. To. ABeeper() ) { put. Beeper();

To this I say. pick. Beeper() if (next. To. ABeeper() ) { put. Beeper(); return false; } else // not next to a beeper { put. Beeper(); return true; } karel_part 4_functions_2 5

Here’s the complete function public boolean exactly. Two. Beepers. On. Corner() { if (next.

Here’s the complete function public boolean exactly. Two. Beepers. On. Corner() { if (next. To. ABeeper() ) { pick. Beeper(); if ( next. To. ABeeper() ) { pick. Beeper() if (next. To. ABeeper() ) { put. Beeper(); return false; //next to a third beeper if it gets here } else { put. Beeper(); return true; //not next to a third beeper, but exactly two if it //gets here } } put. Beeper(); //next to only one beeper if it gets here } return false; //not next to any beepers or only one if it gets here } 6 karel_part 4_functions_2

A Question for You • Write a method that returns true only if there

A Question for You • Write a method that returns true only if there is one or two Beepers on the corner! public boolean one. Or. Two. Beepers. On. Corner() { return ? ? ? ? ; } karel_part 4_functions_2 7

A Question for You • Write a method that returns true only if there

A Question for You • Write a method that returns true only if there is one or two Beepers on the corner! public boolean one. Or. Two. Beepers. On. Corner() { return next. To. ABeeper() || exactly. Two. Beepers. On. Corner(); } karel_part 4_functions_2 8

A Question for You • Does the method work as intended? • Why does

A Question for You • Does the method work as intended? • Why does it fail? • is. Next. To. ABeeper() returns true if next to one, two, three, …. Beepers • How do we fix it? • Replace is. Next. To. ABeeper() with the method: next. To. One. Beeper() – Which we need to write. karel_part 4_functions_2 9

A Question for You • This method has NO parameters and returns true only

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 one. Or. Two. Beepers. On. Corner() { return next. To. One. Beeper() || exactly. Two. Beepers. On. Corne r(); } karel_part 4_functions_2 10

A Question for You public boolean next. To. One. Beeper() { if ( next.

A Question for You public boolean next. To. One. Beeper() { if ( next. To. ABeeper() ) { pick. Beeper() if ( next. To. ABeeper() ) { put. Beeper(); return false; } else { put. Beeper(); return true; } } return false } karel_part 4_functions_2 11

A Karel Example • This method has NO parameters and returns true only if

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 wall. On. Left() { turn. Left(); if ( is. Front. Clear() ) { turn. Right(); return false; } turn. Right(); return true; } karel_part 4_functions_2 12

You try one • Implement the method which returns the number of times a

You try one • Implement the method which returns the number of times a Robot must turn left to face north. num. Left. Turns 2 Face. North public int num. Left. Turns 2 Face. North() • The Robot must be facing the original direction upon completion of the method(function)! karel_part 4_functions_2 13

Your Assignment • Implementing all methods declared in Function. Robot class. • You can

Your Assignment • Implementing all methods declared in Function. Robot class. • You can invoke Main. Driver 1 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 Main. Driver 2 and be amazed! • See handout (Karel_part 4_function. doc) for details. karel_part 4_functions_2 14