announcements announcements
play

Announcements Announcements Reading for Wednesday(Sep 21) The - PowerPoint PPT Presentation

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


  1. 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

  2. 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%

  3. 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

  4. 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…

  5. 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.

  6. 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.

  7. 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(); }

  8. 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(); } }

  9. Review: IF-ELSE example Review: IF ELSE example if(this.frontIsClear()) if(this.frontIsClear()) { this move(); this.move(); } else { this.turnLeft(); }

  10. Review: Comparison Operators Review: Comparison Operators • == == • != • < • > • <= • >= >

  11. 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

  12. 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.

  13. WHILE loop examples WHILE loop examples while(this.countThingsInBackpack() != 0) { this.putThing(); } while(!this.isFacingWest()) { this.turnLeft(); }

  14. 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()

  15. Running Hurdles Running Hurdles

  16. 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.

  17. More Decision Making More Decision Making Robots Learning to Program with Java Learning to Program with Java Byron Weber Becker chapter 5 h 5

  18. 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

  19. 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

  20. 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(); }

  21. 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++; }

  22. 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

  23. 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)

  24. 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

  25. 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

  26. 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

  27. 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++; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend