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.

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Copyright, Joseph Bergin
Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
COMP More About Classes Yi Hong May 22, 2015.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Karel the Robot A Gentle Introduction to the Art of Programming.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Karel J. Robot Chapter 6 Instructions That Repeat.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J Robot Chapter 5.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
Karel J. Robot Chapter 6 Instructions That Repeat.
CS 106A, Lecture 3 Problem-solving with Karel
Karel – Primitive Instructions
CSC111 Quick Revision.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
karel_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
Section 3.2c Strings and Method Signatures
Karel the Robot – Making Decisions
slides courtesy of Eric Roberts
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
CH5 – Conditional Statements
Karel – Primitive Instructions
Presentation transcript:

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 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 karel_part4_functions.ppt Functions return a value Functions that do NOT return a value are called procedures

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

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

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 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 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 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 karel_part4_functions.ppt Homework #1 Now would be a good time to do the first homework assignment for this section!

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 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 karel_part4_functions.ppt To this I say. pickBeeper() if (nextToABeeper() ) { putBeeper(); return false; } else // not next to a beeper { putBeeper(); return true; }

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