Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "For Monday Read Becker, chapter 4, sections 1 and 2."— 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

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

6 Breaking up the Work

7 Breaking up deliverOneAvenue

8 Breaking up deliverOneSide

9 deliverBlock

10 So what have we done here?

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

12 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

13 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

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

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

16

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

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

19

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

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

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

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

24 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


Download ppt "For Monday Read Becker, chapter 4, sections 1 and 2."

Similar presentations


Ads by Google