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)

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.
Copyright, Joseph Bergin
Karel J Robot Chapter 6.
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.
Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.
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.
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.
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
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.
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.
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.
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
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.
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 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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.
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
Eric Roberts and Jerry Cain
Administrative Matters
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
The Little Crab Scenario
Karel the Robot – Making Decisions
SSEA Computer Science: CS106A
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 Chapter 5 Karel J Robot

2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)

3 Chapter 5 IF if ( ) { … } frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); Robot “predicates” either T or F Note: nextTo means on the same corner (not a block away)

4 Chapter 5 Robot Class Then: public class ___ extends UrRobot Now: public class ___ extends Robot The Robot Class is provided by karel. It extends UrRobot Now I have a brain!

5 Chapter 5 Robot Class public class Robot extends UrRobot { public boolean frontIsClear() {…} public boolean nextToABeeper() {…} public boolean nextToARobot() {…} etc… } Now I have a brain! Note: boolean, not void. Returns boolean values (true or false) Remember, you don’t write the Robot class

6 Chapter 5 Examples if (frontIsClear() ) { move();// no danger of hitting wall } if ( anyBeepersInBeeperBag() ) { putBeeper();// no danger of error }

7 Chapter 5 public boolean beeperIsToLeft() { turnLeft(); move(); if ( nextToABeeper() ) { turnLeft(); turnLeft(); move(); turnLeft(); return true; } turnLeft(); turnLeft(); move(); turnLeft(); return false; } MUST put world back in initial situation that it was in BEFORE the method was invoked Pg 107

8 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (! frontIsClear()) { turnLeft(); } move(); ! = Not || = Or && = And Pg

9 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (frontIsClear() && (nextToABeeper()) { pickBeeper(); turnLeft(); } move(); ! = Not || = Or && = And Pg

10 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (frontIsClear() || (nextToABeeper()) { turnLeft(); } move(); ! = Not || = Or && = And Pg

11 Chapter 5 IF - ELSE Flavor 2: if ( ) { } else { }

12 Chapter 5 IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; } simplify: if ( frontIsClear() ) { return true; } return false;

13 Chapter 5 Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();

14 Chapter 5 Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); } move(); if ( beeperOnLeft() ) { turnLeft(); } else { turnRight(); }

15 Chapter 5 no need to check for the same thing twice! if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } if ( facingNorth() ) { move(); pickTwoBeepers(); turnLeft(); }

16 Chapter 5