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

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
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.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
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.
Robot? What’s a Robot? Introducing Karel-the-Robot.
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)
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
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.
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 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.
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.
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.
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
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
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
Nested If Statements While Loops
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 CH5 – Conditional Statements Version 1: if ( ) { } For now: these are method invocations (see next slide) Boolean expressions return true or false

3 if statement if ( ) { … } frontIsClear() nextToABeeper() nextToARobot() facingNorth() facingSouth() facingEast() facingWest() anyBeepersInBeeperBag() Robot “predicates” either true or false Let’s check the API to see our options

4 Robot Class public class Robot extends UrRobot { public boolean frontIsClear() {…} public boolean nextToABeeper() {…} public boolean nextToARobot() {…} etc… } Now I have a brain! again, you don’t write this class

5 Examples from a client program if ( karel.frontIsClear() ) { karel.move(); // no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper();// no danger of error } The client has to refer to the object it is acting on

6 Examples from an object class if ( frontIsClear() ) { move(); // no danger of hitting wall } if (anyBeepersInBeeperBag() ) { putBeeper();// no danger of error }

7 Extending Robot public class SmartBot extends Robot { public boolean beeperIsToLeft() {…} public boolean twoBeepersOrMoreOnCorner() {…} public void faceEast() {…} } Draw the Inheritance Hierarchy

8 Creating a boolean method public boolean methodName() { // other instructions could go here if ( ) { return true; } return false; } The method must return either true or false Returns true and ends the method Returns false and ends the method

9 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

10 SmartBot Create a SmartBot class that extends Robot You write these methods: beeperIsToLeft() ( see previous slide) beeperIsToRight() (this method will return true if at least one beeper is on corner right of robot) twoBeepersOrMoreOnCorner() (this method will return true if at least two beepers are on corner) Note: you may have to nest if statements – look at page 118 in Karel textbook faceEast(), faceNorth(), faceSouth(), faceWest() (these methods will force the robot to turn in the indicated direction) Note: Look at page 112 in Karel textbook Also use the SmartBotTester to test SmartBot and these new methods. You should download and use the world file named smartbotWorld.txt.

11 SmartBot rubric TaskAbove StandardStandardBelow Standard beeperIsToLeft() Returns true if at least one beeper is on a corner left of where the robot is facing. Returns false otherwise. Named correctly Returns true or false correctly in all situations Has no side effects Named differently but the name makes sense (-3 pts) Returns true or false correctly in most situations (-5 pts) May have a side effect (-5 pts) Named incorrectly and name does not make sense (-5 pts) Does not return true or false correctly in most situations (-10 pts) Has side effects (-5 pts) twoBeepersOrMoreOnCorner() Returns true if there are at least two beepers on the corner where the robot is standing. Returns false otherwise. Named correctly Returns true or false correctly in all situations Has no side effects Written optimally Named differently but the name makes sense (-3 pts) Returns true or false correctly in most situations (-5 pts) May have a side effect (-5 pts) May not be written optimally, but works (-3 pts) Named incorrectly and name does not make sense (-5 pts) Does not return true or false correctly in most situations (-10 pts) Has side effects (-5 pts) Not written optimally (-5 pts) faceNorth(),faceSouth(), faceEast(), faceWest() Makes the robot face in the specified direction Named correctly Makes the robot face correctly in all situations Has no side effects Named differently but the name makes sense (-3 pts) Makes the robot face correctly in most situations (-5 pts) May have a side effect (-5 pts) Named incorrectly and name does not make sense (-5 pts) Does not make the robot face correctly in most situations (-10 pts) Has side effects (-5 pts) beeperOnLeftAndRight() Returns true if at least one beeper is on a corner to left AND at least one beeper in on corner to right of where the robot is facing. Returns false otherwise. Named correctly Returns true or false correctly in all situations Has no side effects Named differently but the name makes sense (-3 pts) Returns true or false correctly in most situations (-5 pts) May have a side effect (-5 pts) Named incorrectly and name does not make sense (-5 pts) Does not return true or false correctly in most situations (-10 pts) Has side effects (-5 pts) SmartBot turned in on timeSmartBot turned in on assigned date SmartBot turned in late (-8 pts per day late)

12 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

13 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();

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

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

16 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

17 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

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

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

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

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

22 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() { }

23 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 Look at ex. 9 in the book (pages )

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

25 IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; } One option: if ( frontIsClear() ) { return true; } return false; Or even better: return frontIsClear();

26 Simplify if ( ! leftIsBlocked() ) { return true; } else { return false; }

27 Possible Simplifications if ( ! leftIsBlocked() ) { return true; } else { return false; } One option: if ( leftIsBlocked() ) { return false; } return true; Or even better: return ! leftIsBlocked();

28 Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();

29 Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); }

30 Top factoring does not work here if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); } move(); if ( beeperOnLeft() ) { turnLeft(); } else { turnRight(); } Moves to new corner before checking for beeper

31 However, top factoring does work here if ( nextToABeeper() ) { turnLeft(); pickBeeper(); } else { turnLeft(); move(); } turnLeft(); if (nextToABeeper() ) { pickBeeper(); } else { move(); } turnLeft() does not affect whether robot is next to a beeper turnLeft();

32 Being redundant again and again and again ha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } This if statement is redundant

33 Here is better code (unless one of the instructions can cause a change in direction) if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } if ( facingNorth() ) { move(); pickTwoBeepers(); turnLeft(); }