Announcements Announcements • Reading for Wednesday(Sep 21) – The rest of the chapter 5 (not covering 5.5.2 and 5.5.3) for Wednesday • Quiz#3 returned • Program#2 was due today at 3:35PM Program#2 was due today at 3:35PM • Note: If you do not submit many programs, you will not be able to pass this class. • • Program#3 out today Program#3 out today – Explanation of program#3
Reminder: grade Reminder: grade • 2-Midterm exams 20% • Lab Final 10% • Written Final 15% • Lab activities (14 weeks) 20% Lab activities (14 weeks) 20% • Programs ( 8+ programs) 25% • Homework, quizzes, and class participation 10% T t l Total 100% 100%
Review: Queries Review: Queries • Used to answer questions Used to answer questions – Return boolean value – true or false – Return numeric value – int or double Return numeric value int or double – Return String data – Return other object data Return other object data • Predicates are Queries which return Q boolean values
Review: Robot Predicates Review: Robot Predicates • canPickThing() Determine whether • canPickThing() Determine whether this robot is on the same intersection as a thing it can pick up thing it can pick up. • frontIsClear() Can this robot move forward to the next intersection safely? Copy this to the whiteboard…
Review: RobotSE Predicates Review: RobotSE Predicates • isFacingEast() Determine whether the isFacingEast() Determine whether the robot is facing east. • isFacingNorth() Determine whether • isFacingNorth() Determine whether the robot is facing north. • isFacingSouth() Determine whether th() D t i h th i i S the robot is facing south. • isFacingWest() Determine whether the robot is facing west.
Review: Other Robot Queries Review: Other Robot Queries • int countThingsInBackpack() How many y g p things are in this robot's backpack? • int getAvenue() Which avenue is this robot on? on? • int getStreet() Which street is this robot on? • Direction getDirection() Which direction is this robot facing? • double getSpeed() How many milliseconds • double getSpeed() How many milliseconds will this robot take for the next move or turnLeft instruction? • String toString() Every well coded class () E ll d d l St i t St i has a toString method.
Review: IF that uses a predicate Review: IF that uses a predicate if(this.frontIsClear()) if(this.frontIsClear()) { this.move(); this.move(); } if(!this.frontIsClear()) { this.turnLeft(); }
review: IF that uses the calling object and a non-predicate query di t public void faceWest() { if(this.getDirection() == Direction.NORTH) { this.turnLeft(); } if(this.getDirection() == Direction.EAST) { this.turnAround(); } if(this getDirection() == Direction SOUTH) if(this.getDirection() == Direction.SOUTH) { this.turnRight(); } }
Review: IF-ELSE example Review: IF ELSE example if(this.frontIsClear()) if(this.frontIsClear()) { this move(); this.move(); } else { this.turnLeft(); }
Review: Comparison Operators Review: Comparison Operators • == == • != • < • > • <= • >= >
Review: Pattern for WHILE statement while( «test» ) while( «test» ) { «statements to repeat» t t t t t } • braces surround the statements to repeat and line up with while p • the statements to repeat are indented uniformly uniformly
Four Steps to Building a WHILE Loop Four Steps to Building a WHILE Loop • Identify the one test that must be true Identify the one test that must be true when the looping should stop • Use the opposite form of the test identified • Use the opposite form of the test identified in step 1 as the loop «test». • Within the while , make progress toward Withi th hil k t d completion of the while . • Do whatever is required before or after the while statement is executed to ensure that we solve the given problem.
WHILE loop examples WHILE loop examples while(this.countThingsInBackpack() != 0) { this.putThing(); } while(!this.isFacingWest()) { this.turnLeft(); }
Writing Predicates Writing Predicates • If you are writing a predicate you do not If you are writing a predicate, you do not always need an if statement • The Robot class provides a predicate • The Robot class provides a predicate frontIsClear() • Suppose we write a new predicate frontIsBlocked()
Running Hurdles Running Hurdles
Problem Problem • Write a method to make a Robot go Write a method to make a Robot go completely around the inside of a box created by walls created by walls.
More Decision Making More Decision Making Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker chapter 5 h 5
Chapter Objectives Chapter Objectives • Designing while-loops Designing while loops • Avoiding common errors • Using temporary variables U i t i bl • Nested loops • Introducing for-loops
Common Errors Common Errors • Endless or infinite loops are the most Endless or infinite loops are the most common error type • Omission errors are common on problems • Omission errors are common on problems like the fence post problem – The first and last positions are typically where Th fi t d l t iti t i ll h omission errors occur – Attempting to fix an omission error can cause Attempting to fix an omission error can cause an overrun error – Use the loop and a half pattern Use the loop and a half pattern
Infinite Loop Infinite Loop • What is wrong with this loop? What is wrong with this loop? while(this isFacingNorth()) while(this.isFacingNorth()) { this pickThing(); this.pickThing(); this.move(); }
Another Infinite Loop Another Infinite Loop • What is wrong with this loop? What is wrong with this loop? int count = 10; while(count > 0) hil ( t 0) { this.putThing(); this.move(); this.move(); count++; }
Sample Problem Sample Problem • Use a loop to move a robot along a wall Use a loop to move a robot along a wall, ending just past the end • The wall is of an unknown length • The wall is of an unknown length
Writing a Predicate Writing a Predicate • Predicates return a boolean value Predicates return a boolean value • Used in conditional statements – if(condition) if( diti ) – while(condition) • Can be negated – if(!condition) – while(!condition)
Write a Predicate Write a Predicate • Write a predicate that could be used to Write a predicate that could be used to solve this problem called wallOnRight • Recode the solution • Recode the solution
Temporary Variables Temporary Variables • Declare all* variables at the top of the Declare all variables at the top of the method • Avoid declaring variables inside control Avoid declaring variables inside control structure bodies • Use clear, meaningful names Use clear, meaningful names • Do not use one-letter* or letter-digit variable names variable names * One exception is the counter variable that One exception is the counter variable that controls for-loops
Sample Problem Sample Problem • Somewhere in front of your robot is an Somewhere in front of your robot is an intersection with an unknown number of things • Write code to count the number of things on the g intersection – Display the number being picked up on a label using setLabel(String) • Repeat this (move to the next intersection with things, count them, displaying the number of thi t th di l i th b f things being picked up) until the robot reaches a wall wall
Tracing code with variables Tracing code with variables • What is stored in value at the end of this What is stored in value at the end of this loop? int value = 1; int value 1; int count = 0; while(count < 5) while(count < 5) { value += value*2; l l *2 count++; }
Recommend
More recommend