1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer.

Slides:



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

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
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
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.
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.
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.
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 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.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
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 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
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
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.
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)
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 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.
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
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_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
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
Nested If Statements While Loops
Chapter 3: Selection Structures: Making Decisions
CSE 111 Karel the Robot.
Karel – Primitive Instructions
Presentation transcript:

1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com

2 Paying Attention? For the last several slides, we’ve been using a new robot class. By now you’ve probably figured out that our Inheritance Structure looks like this: UrRobot Robot SmartBot What annoying thing (should have) happened to you while coding the last few examples? Yep, you wrote (or wanted to) turnAround() and maybe even turnRight() AGAIN! ANNOYING! Solution(s)? BetterRobot

3 What if there is only one statement in the THEN clause? if ( frontIsClear()) { move(); } is the same as ….. if ( frontIsClear()) move(); if ( frontIsClear()) { move(); turnLeft(); } is NOT the same as ….. if ( frontIsClear()) move(); turnLeft();

4 Nested IF (IF statement inside an IF statement) if ( frontIsClear()) { move(); if ( nextToABeeper()) { pickBeeper(); } }

5 public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; }

6 0 Beepers 1 Beeper 2 Beepers public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; } Check the lines of code that would execute for each scenario

7 0 Beepers 1 Beeper 2 Beepers public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; } Check the lines of code that would execute for each scenario

8 Boolean Operators Java uses same boolean operators as C++ ( &&, ||, !) && means AND || means OR ! means NOT Example: if (! frontIsClear() || facingSouth()) { turnLeft(); } move();

9 if (frontIsClear() && nextToABeeper()) frontIsClear() nextToABeeper()true truefalse falsetruefalse if (frontIsClear() || nextToABeeper()) frontIsClear() nextToABeeper()true truefalse falsetruefalse result true false result true false

10 IF - ELSE Version 2: if ( ) { } else { }

11 IF - ELSE Example: if ( beeperIsToLeft() ) { turnLeft(); move(); pickBeeper(); } else { move(); }

12 Practice Using && and || Write this method which could be put in SmartBot /* returns true if there is at least one beeper on both sides of bot, false otherwise */ public boolean beeperOnLeftAndRight() { }

13 Create a single followWallRight() method to handle each of these situations. Hint: Before coding, look at the four situations and see what is the same and different for each. Start with the initial situation for each robot. How could you use an if statement to determine if they are in a specific situation? This can be done with an if statement that includes nested if statements. Initial Situation End Situation