1 / 24

For Monday

For Monday. Read Becker, chapter 4, sections 1 and 2. Program 2. Delivering Flyers.

rladd
Download Presentation

For Monday

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 Monday • Read Becker, chapter 4, sections 1 and 2

  2. Program 2

  3. Delivering Flyers • You need a robot to deliver flyers to houses on a delivery route. The route is shown on the next slide. The robot must visit all of the houses on the route and should stay off the green areas as much as possible.

  4. What route? • This shows a possible route (not counting the actual delivery to the houses)

  5. Breaking up the Work

  6. Breaking up deliverOneAvenue

  7. Breaking up deliverOneSide

  8. deliverBlock

  9. So what have we done here?

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

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

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

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

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

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

  16. Using Threads • What if we could have our robots do things at the same time?

  17. public class DeliveryBot extends RobotSE implements Runnable { public DeliveryBot(City aCity, int aStr, int anAve, Direction aDir, int numFlyers) { super(aCity, aStr, anAve, aDir, numFlyers); } // The run method contains the code to be executed within the thread. public void run() { this.deliverBlock(); } public void deliverBlock() { this.deliverHouse(); this.deliverHouse(); . . . } . . . }

  18. import becker.robots.*; public class DeliverFlyers { public static void main(String[ ] args) { // Same as before Route route = new Route(); DeliveryBot db1 = new DeliveryBot(route, 0, 0, Direction.EAST, 6); DeliveryBot db2 = new DeliveryBot(route, 6, 0, Direction.EAST, 6); . . . db1.deliverBlock(); db2.deliverBlock(); . . . // Set up to run db1 and db2 in parrallel Thread db1Thread = new Thread(db1); Thread db2Thread = new Thread(db2); . . . // Start executing the code in run() db1Thread.start(); db2Thread.start(); . . . } }

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

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

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

More Related