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.

Slides:



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

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
Program: Little Crab Mr Gano.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Visual C++ Programming: Concepts and Projects
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.
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.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
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.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
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.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
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.
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 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.
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
CS 106A, Lecture 2 Programming with Karel
IF if (condition) { Process… }
SSEA Computer Science: CS106A
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
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:

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 and make decisions about what to do next – The IF and the IF/ELSE provide robots with decision making abilities Robot programs contain several different kinds of instructions – Messages to robots, either primitives or new instructions – Constructors (specifications on how to build a robot) – Control statements: IF and IF/ELSE are the first examples of these

The IF instruction General form if ( ) { } Robot determines whether is true or false is a sequence of any valid robot commands that will be performed when the robot determines the is true Nothing will happen when the is false

Example of IF statement // in a method// in a main task block if (nextToABeeper())if (Karel.nextToABeeper()){ pickBeeper(); Karel.pickBeeper();} turnLeft();Karel.turnLeft(); A beeper will be picked up only if there is a beeper on the same corner as the robot – Dependent upon the results of the test The robot will turn left, regardless of whether a beeper is on the corner or not – Independent of the results of the test

Conditions that a robot can test class Robot extends ur_Robot { boolean frontIsClear(); boolean nextToABeeper(); boolean nextToARobot(); boolean facingNorth(); boolean facingSouth(); boolean facingEast(); boolean facingWest(); boolean anyBeepersInBeeperBag(); }

Class Robot Inherits all instructions from ur_Robot We will use the Robot class as our base robot from now on The new methods of the class are predicates – Preceded by the boolean return type – These methods return either true or false – (functions such as move() or turnLeft() have a void return type, because they return no information to the robot; merely cause the robot to behave in a particular way)

Negative conditions? Suppose we want a negative form of a predicate? We can precede a predicate with the negative operator ! if (! Karel.next-To-A-Beeper()) { // stuff }

New predicate instructions We could create a new subclass of the robot class and provide a new predicate method class myRobot extends Robot {// assume constructor boolean frontIsBlocked() { return ! frontIsCLear(); } // more stuff }

RETURN statement In the block of the definition of a new predicate we need to indicate what value is to be returned We need to use the reserved word return, followed by an expression In a predicate method the value of the expression must be true or false

Use of the new predicate void task() { myRobot R = new myRobot(1, 1, North, 0); if (R.frontIsBlocked()) { // stuff } // and so on }

Walls to the right or left? Suppose we needed to determine if there is a wall to the right or left? boolean leftIsClear() { turnLeft(); if (frontIsClear()) { turnRight();// assume definition return true; // method terminates here } turnRight(); return false; } Return statement immediately terminates the method

Sparse Harvester Demo The field to be harvested has been hit by a storm, so that not every corner has a beeper. Let us use conditionals to modify Harvester to solve this problem Download SparseHarvester demo will not run correctly because of an error shutoff

For practice – two methods faceNorthIfFacingSouth() faceNorth()

The IF/ELSE instruction General form if ( ) { // note lack of semicolon } else { // note lack of semicolon } Robot determines whether is true or false is a sequence of any valid robot commands that will execute when the robot determines the is true will execute when the is false

Racer Robot A hurdle jumping race A robot will run a mile. The course may or may not have an obstacle (wall) in the path. If the robot encounters a wall, it will go around (hurdle) the wall. Otherwise it will continue on it’s path… Download Racer demo

Nested IF Instructions Written with an IF instruction nested inside the THEN or ELSE clause of another IF As an example: if ( ) { } else { } } else { }

The Replanter Robot The field to be harvested has either 0, 1 or 2 beepers on each corner. Our robot must make sure that each corner has only one and only one beeper on each corner Download Replanter demo

When to Use an IF Instruction The IF instruction allows a robot to decide whether to execute or skip entirely the block of instructions within the THEN clause The IF/ELSE instruction allows a robot to decide whether to execute or skip the block of instructions within the THEN clause or the ELSE clause Nesting these instructions allows robots to make more complex choices

The Decision Map

Transformations for Simpler IF Instructions Transformations can make our programs smaller, simpler, more logical, and more readable. Two program fragments that result in exactly the same behavior is execution equivalent turnLeft();putBeeper(); putBeeper();turnLeft(); We can create an execution equivalent IF/ELSE from another by replacing with it’s opposite and interchanging the THEN and ELSE clauses

Test Reversal if (frontIsClear()) {if (!frontIsClear()){ move();jumpHurdle();} else { jumpHurdle();move();} Used to solve the following difficulty if ( ) {if (! ) { doNothing(); ; } else {} ; } // doNothing() is a method that // literally does nothing

Bottom Factoring if ( ) {if ( ){<instruction1>; ; } else { } else { ; ; } ; ; } These two IF statements are execution equivalent

Top Factoring if ( ) { ; ; if ( ){ ; ; } else { ; ; ; } } These two IF statements are execution equivalent

Analyze these statements if (nextToABeeper()) { move(); move(); if (nextToABeeper()){ turnLeft(); } else { move(); turnRight(); turnRight(); } } Are these two IF statements are execution equivalent? General rule: top factor only when test conditions between the original and the factored versions of the instructions do not change

Redundant Test Factoring if (facingWest()) { if (facingWest()){ move(); if(facingWest()){ turnLeft(); turnLeft(); } } The nested IF on the left is redundant and not needed Potential difficulty: intervening instructions might change Karel’s position in an unknown way