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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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
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.
You ARE NOT ALLOWED To Copy Files!!!. You ARE NOT ALLOWED To Be Told What to Write in the Program.
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 In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
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.
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.
Karel as a Turing Machine CSE /03/04. Facts Any programming language which satisfies Boehm & Jacopini's conditions can be expressed by means of.
Robot? What’s a Robot? Introducing Karel-the-Robot.
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 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)
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.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
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.
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.
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 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.
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
CS 106A, Lecture 3 Problem-solving with Karel
Karel – Primitive Instructions
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.
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.
CS 106A, Lecture 2 Programming with Karel
Algorithm The key is the step-by-step instructions.
Karel the Robot – Making Decisions
SSEA Computer Science: CS106A
A Gentle Introduction to the Art of Object Oriented Programming
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 -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright Move ahead 5 streets Turn right Move ahead 5 streets Turn right Move ahead 5 streets Turn right Move ahead 5 streets Turn right Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE beginning-of-program DEFINE-NEW-INSTRUCTION turnright AS BEGIN turnleft; END; DEFINE-NEW-INSTRUCTION move- ahead-5 AS BEGIN move; END; beginning-of-execution move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE The ITERATE command enables the programmer to have Karel repeat an instruction or set of instructions a fixed number of times. ITERATE TIMES The can actually be a group of instructions enclosed by a BEGIN and END Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE Look at the definition for turnright DEFINE-NEW-INSTRUCTION turnright AS BEGIN turnleft; END; We could write this as follows: DEFINE-NEW-INSTRUCTION turnright AS BEGIN ITERATE 3 TIMES turnleft; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE Look at our program to have Karel walk around the block. The definition move-ahead-5 could be modified as follows: DEFINE-NEW-INSTRUCTION move-ahead-5 AS BEGIN ITERATE 5 TIMES move; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE Even withing the main program we repeat a lot of statements. beginning-of-execution move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; turnoff; Many of these instructions could be replaced by an ITERATE statement. beginning-of-execution ITERATE 4 TIMES BEGIN turnright; move-ahead-5; END; turnoff; Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE Problem Statement: Karel has 5 beepers in his beeper bag. His task is to make sure that the five corners between 3rd Street and 3rd Avenue and 3rd Street and 7th Avenue all have one beepers. Two of these corners already have beepers on them. Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE Revised Algorithm Pickup 5 beepers Move 2 avenue blocks If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE beginning-of-program beginning-of-execution pickbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

Karel the Robot -- ITERATE beginning-of-program beginning-of-execution ITERARE 5 TIMES pickbeeper; move; ITERATE 4 TIMES BEGIN IF not-next-to-a-beeper THEN putbeeper; move; END; IF not-next-to-a-beeper THEN putbeeper; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner