1 / 20

For Wednesday

For Wednesday. Read Becker, chapter 4, sections 3-5 Complete WebCT quiz. Program 2. Any questions?. Factoring out Differences. What if I want a CollectionBot? What do I need to change? How do I avoid duplicating code?. Helper Methods. What is a helper method?

fblake
Download Presentation

For Wednesday

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For Wednesday • Read Becker, chapter 4, sections 3-5 • Complete WebCT quiz

  2. Program 2 • Any questions?

  3. Factoring out Differences • What if I want a CollectionBot? • What do I need to change? • How do I avoid duplicating code?

  4. Helper Methods • What is a helper method? • Who should call a helper method? • How do we enforce that?

  5. Access Modifiers • Public • Access from anywhere • Use for the services a robot provides to others • Protected • Access from methods in the class or its subclasses • Use for methods that may be overridden but don’t need to be public • Private • Access from only methods in the class • Use for anything that doesn’t need to be public or protected

  6. Control Structures • Allow programs to make decisions • Why the name “control structure”?

  7. Motivation • So far, we’ve written programs that • executed one statement after another (pattern?) • executed all statements in a method and returned • Some problems can’t be solved this way • Have a robot move forward until it reaches a wall. • Have a robot pick up things from intersections when not all intersections have things on them • Other examples? • Robot • Non-robot

  8. Different Kinds of Decisions • Should this group of statements be executed once, or not at all? if (karel.canPickThing()) { karel.turnLeft(); } karel.move(); • Should this group of statements be executed again? while (karel.canPickThing()) { karel.turnLeft(); } karel.move();

  9. Tracing an if statement if (karel.frontIsClear()) { karel.move(); } karel.turnLeft();

  10. Tracing a while statement while (karel.frontIsClear()) { karel.move(); } karel.turnLeft();

  11. What’s the Pattern?

  12. Pseudocode and Decisions

  13. Questions Robots Can Ask • Can I pick up a Thing from this intersection? • How many Things are in my backpack? • Is the path in front of me clear? • What avenue am I on? • What street am I on? • What direction am I facing? • What is my speed? • What is my current label?

  14. Predicates • Questions with the answers yes or no (actually true or false) • Can be directly used in if and while statements. • Return values of type boolean.

  15. Negatives • Sometimes we want to test the opposite of a predicate • Use the symbol !

  16. Integer Queries if (karel.getStreet() == 1) { karel.turnAround(); } while (karel.countThingsInBackpack < 4) { karel.pickThing(); }

  17. Comparison Operators • == • != • < • > • <= • >=

  18. Practice • Write code to move two spaces forward, picking up any things in any space the robot enters.

  19. Problem • Write a method to make a Robot go completely around the inside of a box created by walls.

More Related