CH5 – Conditional Statements

Slides:



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

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.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java.
Chapter 2 - The First Program: Little Crab
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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)
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.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
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 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 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
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.
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.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
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.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
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
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
Ch.3 Classes & Stepwise Refinement
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 J Robot.
karel_part4_functions_2
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
CS 106A, Lecture 6 Control Flow and Parameters
The Little Crab Scenario
SSEA Computer Science: CS106A
slides courtesy of Eric Roberts
Ch.3 Classes & Stepwise Refinement
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Object References a.k.a. variables
Nested If Statements While Loops
Ch.3 Classes & Stepwise Refinement
Ch.3 Classes & Stepwise Refinement
OPERATORS AND SELECTION 2
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

CH5 – Conditional Statements Flavor 1: if ( <some boolean expression> ) { <some instruction list> } For now: these are method invokations (see next slide) Chapter 5 - IF

Let’s check the API to see our options IF “predicates” either Tor F frontIsClear() nextToABeeper() nextToARobot() facingNorth() facingSouth() facingEast() facingWest() anyBeepersInBeeperBag() if ( ) { … } Robot Let’s check the API to see our options Chapter 5 - IF

again, you don’t write this class 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 Chapter 5 - IF

Examples if ( karel.frontIsClear() ) // in driver { karel.move(); // no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) // in driver { karel.putBeeper(); // no danger of error } Chapter 5 - IF

Draw the Inheritance Hierarchy Extending Robot public class SmartBot extends Robot { public boolean beeperIsToLeft() {…} public boolean twoBeepersOnCornerOrMore() {…} public void faceEast() {…} } Draw the Inheritance Hierarchy Chapter 5 - IF

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 Chapter 5 - IF

you write twoBeepersOnCornerOrMore() it belongs to the SmartBot class note: you may have to nest if statements Chapter 5 - IF

you write faceEast() it belongs to the SmartBot class Chapter 5 - IF

Paying Attention? For the last several slides, I’ve lead you somewhat astray. By now (I’m hoping) someone has pointed out that our Inheritance Structure looks likes this What annoying thing (should have) happened to you while coding the last few examples? UrRobot Robot SmartBot Yep, you wrote (or wanted to) turnRight() and maybe even turnAround() AGAIN! ANNOYING! Solution(s)? Chapter 5 - IF

Boolean Operators Karel (Java) Robot (&&, ||, !) if (! frontIsClear()) { turnLeft(); } move(); // always going to move True  turnLeft and move False  move Ex) if (facingEast() || nextToABeeper()) if (!frontIsClear() && nextToARobot()) Chapter 5 - IF

&& and || (no, I didn’t stutter) Write this method /** * @returns true if there is at least * one beeper on both sides of bot, * false otherwise */ public boolean isBeepOnLeftAndBeepOnRight() Assume that the class you’re writing this for has SmartBot as its superclass (the one we just suggested). It may help to look at the inheritance tree again. Chapter 5 - IF

IF - ELSE Flavor 2: if ( <boolean expression> ) { <statements> } else // do this instead <statements – somewhat different> Chapter 5 - IF

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

Simplify if ( ! leftIsBlocked() ) { return true; } else return false; Chapter 5 - IF

Simplify if ( leftIsBlocked() ) { return false; } else return true; Chapter 5 - IF

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

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

being redundant again and again and againha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) turnLeft(); } Chapter 5 - IF