for wednesday
play

For Wednesday Read Becker, chapter 4, sections 3-5 Program 2 design - PowerPoint PPT Presentation

For Wednesday Read Becker, chapter 4, sections 3-5 Program 2 design due Program 2 Advantages of Stepwise Refinement Tends to make programs Easier to understand Free of errors Easier to test and debug Easier to modify


  1. For Wednesday • Read Becker, chapter 4, sections 3-5 • Program 2 design due

  2. Program 2

  3. Advantages of Stepwise Refinement • Tends to make programs – Easier to understand – Free of errors – Easier to test and debug – Easier to modify • Why?

  4. The Reasons • People can’t keep all the details in their heads at once. • Helps to impose structure on the problem • The descriptive names allow us to ignore details

  5. Pseudocode Algorithms • Let us focus on the algorithm, not the details of Java, first • Combines natural language with structure • Very important as we move on to writing programs that make decisions next week. • Example: deliver fliers to each house up to the corner turn the corner deliver fliers to each house up to the corner turn the corner deliver to the last house

  6. Advantages of Pseudocode • Pseudocode helps us think more abstractly, allowing us to ignore many irrelevant details. • Pseudocode allows us to trace our programs very early in development. • Pseudocode can provide a common language on a development team, even with non-technical users. • Algorithms expressed in pseudocode can be implemented in a variety of programming languages.

  7. Using Multiple Robots • How could we do the flyer problem efficiently with multiple robots? • Would we have to change the DeliveryBot code?

  8. import becker.robots.*; public class DeliverFlyers { public static void main(String[ ] args) { Route route = new Route(); DeliveryBot db1 = new DeliveryBot(route, 0, 0, Direction.EAST, 6); DeliveryBot db2 = new DeliveryBot(route, 6, 0, Direction.EAST, 6); DeliveryBot db3 = new DeliveryBot(route, 5, 5, Direction.WEST, 6); DeliveryBot db4 = new DeliveryBot(route, 11, 5, Direction.WEST, 6); DeliveryBot db5 = new DeliveryBot(route, 0, 6, Direction.EAST, 6); DeliveryBot db6 = new DeliveryBot(route, 6, 6, Direction.EAST, 6); DeliveryBot db7 = new DeliveryBot(route, 5, 11, Direction.WEST, 6); DeliveryBot db8 = new DeliveryBot(route, 11, 11, Direction.WEST, 6); db1.deliverBlock(); db2.deliverBlock(); db3.deliverBlock(); db4.deliverBlock(); db5.deliverBlock(); db6.deliverBlock(); db7.deliverBlock(); db8.deliverBlock(); }

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

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

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

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

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

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

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

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

  17. What’s the Pattern?

  18. Pseudocode and Decisions

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

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

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

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

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

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

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

Recommend


More recommend