Download presentation

Presentation is loading. Please wait.

Published byReynaldo Brow Modified over 2 years ago

1
1 karel_part4_functions.ppt 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.

2
2 karel_part4_functions.ppt Functions return – a value Number Boolean value Words Direction – nothing –An Object A Robot A Direction Penguin –State of an Object Height Number of beepers

3
3 karel_part4_functions.ppt Functions return a value Functions that do NOT return a value are called procedures

4
4 karel_part4_functions.ppt Functions Functions may calculate a value, e.g. –Circumference –Volume –Distance between Objects –Elapse Time –Etc.

5
5 karel_part4_functions.ppt Functions Functions may return a known value, e.g. –Color –size

6
6 karel_part4_functions.ppt User Define Function In java (or Karel) new function can be created! public returnType methodName(parameters list) –returnType indicate what is being returned by this function. –If returnType is void, the function returns nothing and is called a procedure. –methodName is the name of the function. –parameters list is any additional information needed to return the desired results! Parameter list may be empty or contain pairs of words indicating the type (number, word, etc and its reference)

7
7 karel_part4_functions.ppt Example The following method has two parameters, s1 and s2 and returns one string that is built by concatenating s1 & s2 public String combine(String s1, String s2) { String str = s1 + s2; return str; } Sample call String ans = combine(“Computer”, “Science”); Sets ans to “ComputerScience”

8
8 karel_part4_functions.ppt Another Example The following method has three parameters, s1, s2 and s3 and returns the sum s1 + s2 + s3 public int sum(int s1, int s2, int s3) { int ans = s1 + s2 + s3; return ans; } Sample call int ans = sum(3, 20, 100); Sets ans to 123 int ans = sum(-13, 13, 5); Sets ans to 5

9
9 karel_part4_functions.ppt Another Example The following method has three parameters, s1, s2 and s3 and returns the sum s1 + s2 + s3 public int sum(int s1, int s2, int s3) { return s1 + s2 + s3; } Sample call int ans = sum(3, 20, 100); Sets ans to 123 int ans = sum( sum(-10,-3,0), 13, 5); Sets ans to 5

10
10 karel_part4_functions.ppt Homework #1 Now would be a good time to do the first homework assignment for this section!

11
11 karel_part4_functions.ppt 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; }

12
12 karel_part4_functions.ppt 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; }

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

14
14 karel_part4_functions.ppt Here’s the complete function public boolean exactlyTwoBeepersOnCorner() { if (nextToABeeper() ) { pickBeeper(); if ( nextToABeeper() ) { pickBeeper() if (nextToABeeper() ) { putBeeper(); return false; //next to a third beeper if it gets here } else { 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 }

15
15 karel_part4_functions.ppt 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 ???????; }

16
16 karel_part4_functions.ppt 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(); }

17
17 karel_part4_functions.ppt 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.

18
18 karel_part4_functions.ppt 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(); }

19
19 karel_part4_functions.ppt A Question for You public boolean nextToOneBeeper() { if ( nextToABeeper() ) { pickBeeper() if ( nextToABeeper() ) { putBeeper(); return false } else { putBeeper(); return true; } return false }

20
20 karel_part4_functions.ppt 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; }

21
21 karel_part4_functions.ppt 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)!

22
22 karel_part4_functions.ppt 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.

Similar presentations

OK

CSE 113 Week 3 January 28 – February 1, 2008. Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Bring your laptop or.

CSE 113 Week 3 January 28 – February 1, 2008. Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Bring your laptop or.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on ip address classes ppt Ppt on history of pie in math Ppt on regional trade agreements for italy Ppt on different types of computer softwares Ppt on red fort delhi Ppt on meaning of colors Ppt on table tennis game Ppt on preservation of public property Ppt on duty roster meaning Free convert pdf to ppt online