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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ABC Technology Project
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.
© S Haughton more than 3?
Squares and Square Root WALK. Solve each problem REVIEW:
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Copyright, Joseph Bergin
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
Week 1.
We will resume in: 25 Minutes.
1 Unit 1 Kinematics Chapter 1 Day
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.
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.
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)
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 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.
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.
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 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.
Administrative Matters
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
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
CH5 – Conditional Statements
Karel – Primitive Instructions
Presentation transcript:

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

2 karel_IF_part1 IF if ( ) { … } frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); Robot predicates either True or False

3 karel_IF_part1 What Does it Mean? public boolean frontIsClear() HELP - The word void has been replaced by the word boolean. This Means this method returns either true or false. If there is NO wall in between the Robot and the corner directly in front, this method returns true. Otherwise (there is a wall), this method returns false.

4 karel_IF_part1 Method Definition public boolean nextToABeeper(); If there is a (one or more) Beeper on the Robots current corner, this method returns true. Otherwise (NO Beeper current corner), this method returns false. That is, returns true if safe for Robot to pickBeeper

5 karel_IF_part1 Method Definition public boolean nextToARobot(); If there is another (one or more) Robot on the Robots current corner, this method returns true. Otherwise (NOT another Robot on current corner), this method returns false.

6 karel_IF_part1 Method Definition public boolean anyBeepersInBeeperBag(); If this Robot has any (one or more) beepers in its beeper bag, this method returns true. Otherwise (NO beepers in beeper bag), this method returns false. That is, returns true if safe for Robot to putBeeper

7 karel_IF_part1 Method Definition public boolean facingNorth(); If this Robots is facing North (up), this method returns true. Otherwise (facing South, East or West ), this method returns false.

8 karel_IF_part1 Method Definition public boolean facingSouth(); If this Robots is facing South (down), this method returns true. Otherwise (facing North, East or West ), this method returns false.

9 karel_IF_part1 Method Definition public boolean facingEast(); If this Robots is facing East (to the right), this method returns true. Otherwise (facing North, South or West ), this method returns false.

10 karel_IF_part1 Method Definition public boolean facingWest(); If this Robots is facing West (to the left), this method returns true. Otherwise (facing North, South or East ), this method returns false.

11 karel_IF_part1 Indenting by example How to properly indent while writing if structures by example! public void someMethod() { if ( ) { codeGoesHere(); allAdditionalCodeLinesUp(); soAllFirstLettersAreInTheSameColumn(); } afterClosingBracketNextMethodCallGoesHere(); untilAnotherIfStatementIsEncountered(); }

12 karel_IF_part1 A word about indenting How to properly indent while writing if structures! Every time an if is used, each line after the curly bracket, {, should be spaced over to the right a constant amount (say 3 spaces). –To simplify, the first letter of each method/line-of-code should be under the ( in the if statement. When the if is terminated ( closing curly bracket } ), the following lines of code are moved left by the same number of spaces used above (3 was used above). –To simplify, the first letter of each method/line-of-code should be under the i in the if statement. Please review previous slide

13 karel_IF_part1 A Final Indenting example if ( frontIsClear()) { move(); turnLeft(); } if ( frontIsClear()) { move(); turnLeft(); if ( frontIsClear()) { move(); } turnLeft();

14 karel_IF_part1 Sample Code if (karel.frontIsClear() ) // if (frontIsClear()) { karel.move();// no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper();// no danger of error }

15 karel_IF_part1 Sample Code for Karel10 if (frontIsClear() ) // if (frontIsClear()) { move();// no danger of hitting wall } if (anyBeepersInBeeperBag() ) { putBeeper();// no danger of error }

16 karel_IF_part1 Boolean Operators Robot (Java) (&&, ||, !) –! Not –&& And –|| Or

17 karel_IF_part1 ! (not) Write this method /** true if there is a wall * directly in front of the Robot, * false otherwise. */ public boolean isFrontBlocked () { // enter code

18 karel_IF_part1 What Now? How does the Robot tell if the front direction is blocked? The front being blocked is the opposite of front is clear. We can write isFrontBlock by using the opposite of isFrontClear. Therefore isFrontBlock, looks like: public boolean isFrontBlock() { return !isFrontClear(); }

19 karel_IF_part1 Order of Operations () - Parenthesis ! - not && and || - or A && B || C != A && ( B || C )

20 karel_IF_part1 Your First Assignment In assignment Karel03, you knew that every corner contained a beeper. In Karel10, Swiper the Fox has stolen Beepers from several corners in the world. Your assignment is to implement a new BeeperSweeper Robot that only picks beepers on corners with beepers. See handout ( Karel_10_part1_if.doc ) for more details

21 karel_IF_part1 Your Second Assignment In this assignment, your Robot will follow a secret path by obeying the following rules. 1.The path is 41 steps long. The final corner will have will have a wall on three sides and a Beeper on the corner. 2.The Robot should step according to the following rule. 1. If the Robots path is blocked by a wall, the Robot should turn left. (There will not be a wall blocking the move after turning left). 2.If the Robots is on a corner with a beeper, the Robot should turn right. (There will not be a wall blocking the move after turning right). 3.If the path is not blocked and no beeper was on the corner, move forward one corner. * See handout ( Karel_11_part1_if.doc ) for more details