Karel the Robot – Making Decisions

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
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Nested If Statements While Loops
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.
CMPUT 101 Lab # 2 September 17, :00 – 16:50.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
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.
Using Algorithms Copyright © 2008 by Helene G. Kershner.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Robot? What’s a Robot? Introducing Karel-the-Robot.
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.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
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.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
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.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
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.
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.
ITK 168 Decisions Dr. Doug Twitchell September 20, 2005.
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.
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.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
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)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Karel J. Robot Chapter 6 Instructions That Repeat.
Karel – Primitive Instructions
In the beginning… software
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.
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Using Algorithms Copyright © 2008 by Helene G. Kershner.
Karel J Robot OOP approach to learning computer science
Using Algorithms Copyright © 2008 by Helene G. Kershner.
Algorithm The key is the step-by-step instructions.
A Gentle Introduction to the Art of Object Oriented Programming
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Karel J Robot OOP approach to learning computer science
Algorithm The key is the step-by-step instructions.
Nested If Statements While Loops
CSE 111 Karel the Robot.
Karel – Primitive Instructions
Presentation transcript:

Karel the Robot – Making Decisions To make decisions, Karel uses the IF statement. Karel can determine if a limited set of conditions is True or False. front-is-clear front-is-blocked left-is-clear left-is-blocked right-is-clear right-is-blocked next-to-a-beeper not-next-to-a-beeper any-beepers-in-beeper-bag no-beepers-in-beeper-bag Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Remember what an IF statement look like: IF <test condition is True> THEN BEGIN <instruction(s)>; END; IF not-next-to-a-beeper THEN begin putbeeper; end; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Problem Statement: Karel has been told to place two beepers on each street corner between 1st street and 2nd avenue and 1st street and 10th avenue and return home. This would be a very easy task except there was a beeper party last night and there are beepers scattered around on random street corners. Define Output: There will be two beepers on every corner on 1st street between 2nd avenue and 10th avenue. Karel will be at the origin. Define Input: Karel is next to 20 beepers. He is at the Origin facing east. Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Initial Algorithm Pickup 18 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers . . . turnaround move 9 streets turnoff Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Revised Algorithm Pickup 18 beepers Repeat 9 times Move If next to a beeper, put a beeper down Otherwise put down 2 beepers turnaround move 9 streets turnoff Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions IF / THEN / ELSE IF < test condition> THEN BEGIN <Do these instructions when test = True>; END ELSE <Do these instructions when test =False>; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Looking at our revised algorithm, We have the statement: If next to a beeper, put a beeper down Otherwise put down 2 beepers Anytime we use a word like OTHERWISE in English an IF/THEN/ELSE is used in Karel’s world. Translating our algorithm into Karel’s language would produce: IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE (another word for Otherwise) END; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Looking at Karel’s code IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE (another word for Otherwise) END; 1) When Karen is next-to-a-beeper (the test is TRUE), one beeper get put down. 2) When Karen is NOT next-to-a-beeper (the test is FALSE), two beepers get put down. 3) BEGIN / END statements must match up or Karel (not to mention the programmer) gets very confused. Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions beginning-of-execution ITERATE 18 TIMES BEGIN pickbeeper; END; ITERATE 9 TIMES move; IF next-to-a-beeper THEN putbeeper; END ELSE turnaround; turnoff; end-of-execution Copyright © 2008 by Helene G. Kershner