Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2.5 Top Down Design. © 2006 Pearson Addison-Wesley. All rights reserved 2.5.2 Therefore, problems must often be broken into small parts. Then.

Similar presentations


Presentation on theme: "Lecture 2.5 Top Down Design. © 2006 Pearson Addison-Wesley. All rights reserved 2.5.2 Therefore, problems must often be broken into small parts. Then."— Presentation transcript:

1 Lecture 2.5 Top Down Design

2 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.2 Therefore, problems must often be broken into small parts. Then the small parts can be solved one at a time. Top-down design is one divide and conquer technique for developing an algorithm. Top-down design is similar to outlining. An algorithm is a collection of instructions (possibly expressed informally) that perform some task. Q: Where do I begin? (when writing a program/creating an algorithm)...so creating algorithms is an essential skill for a software developer. Humans (even programmers) have limited intellect. divide conquer Top-down design begins with the top level (like the major points of an outline).

3 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.3 Program Requirements: Draw the following picture.

4 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.4 An Initial algorithm (the top level) // 1) Draw a square. Each of these three steps must be refined. // 3) Draw 4-corners opening out. // 2) Draw tic-tac-toe lines.

5 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.5 Refine Step 1 - draw a square // 1.1) Move to upper left corner headed left. // 1.3) Rotate 90 degrees and draw right edge. // 1.2) Rotate 180 degrees and draw top edge. // 1.4) Rotate 90 degrees and draw bottom edge. // 1.5) Rotate 90 degrees and draw left edge.

6 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.6 Refine Step 2 - draw tic-tac-toe lines // 2.1) Draw left vertical line. // 2.3) Draw top horizontal line. // 2.2) Draw right vertical line. // 2.4) Draw bottom horizontal line.

7 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.7 Refine Step 3 - draw tic-tac-toe lines // 3.1) Draw upper left corner. // 3.3) Draw upper right corner. // 3.2) Draw lower left corner. // 3.4) Draw lower right corner.

8 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.8 Recap - the algorithm as refined to level 2 // 1) Draw a square. // 3) Draw 4-corners opening out. // 2) Draw tic-tac-toe lines. // 1.1) Move to upper left corner headed left. // 1.3) Rotate 90 degrees and draw right edge. // 1.2) Rotate 180 degrees and draw top edge. // 1.4) Rotate 90 degrees and draw bottom edge. // 1.5) Rotate 90 degrees and draw left edge. // 2.1) Draw left vertical line. // 2.3) Draw top horizontal line. // 2.2) Draw right vertical line. // 2.4) Draw bottom horizontal line. // 3.1) Draw upper left corner. // 3.3) Draw upper right corner. // 3.2) Draw lower left corner. // 3.4) Draw lower right corner.

9 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.9 More refinement from Step 1 // 1.1) Move to upper left corner headed left. // 1.2) Rotate 180 degrees and draw top edge. squarePen.turnBy(180); squarePen.moveBy(120); squarePen = new DrawingGizmo(); squarePen.dontDraw(); squarePen.moveBy(60); squarePen.turnBy(-90); squarePen.moveBy(60); squarePen.draw(); // 1.3) Rotate 90 degrees and draw top edge. // 1.4) Rotate 90 degrees and draw top edge. // 1.5) Rotate 90 degrees and draw top edge. squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120);

10 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.10 More refinement from Step 2 // 2.1) Draw left vertical line. leftLinePen = new DrawingGizmo(); leftLinePen.dontDraw(); leftLinePen.moveBy(80); leftLinePen.turnBy(-90); leftLinePen.moveBy(40); leftLinePen.turnBy(-90) leftLinePen.draw(); leftLinePen.moveBy(160); // 2.2) Draw right vertical line. // 2.3) Draw top horizontal line. // 2.4) Draw bottom horizontal line. similar to 2.1

11 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.11 More refinement from Step 3 // 3.1) Draw upper left corner. upperLeftPen = new DrawingGizmo(); upperLeftPen.dontDraw(); upperLeftPen.moveBy(80); upperLeftPen.turnBy(-90); upperLeftPen.moveBy(20); upperLeftPen.turnBy(-90) upperLeftPen.draw(); upperLeftPen.moveBy(60); upperLeftPen.turnBy(-90); upperLeftPen.moveBy(60); // 3.2) Draw lower left corner. // 3.3) Draw upper right corner. // 3.4) Draw lower right corner. similar to 3.1

12 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.12 public class Driver { private DrawingGizmo squarePen, leftLinePen, rightLinePen, topLinePen, bottomLinePen, upperLeftPen, lowerLeftPen, upperRightPen, lowerRightPen; /** post: the image shown to the right has been drawn. */ public Driver() { squarePen = new DrawingGizmo(); squarePen.dontDraw(); squarePen.moveBy(60); squarePen.turnBy(-90); squarePen.moveBy(60); squarePen.dontDraw(); squarePen.turnBy(180); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); // 2) Draw tic-tac-toe lines. // 3) Draw 4-corners opening out. }

13 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.13 public class Driver { private DrawingGizmo squarePen, leftLinePen, rightLinePen, topLinePen, bottomLinePen, upperLeftPen, lowerLeftPen, upperRightPen, lowerRightPen;; /** post: the image shown to the right has been drawn. */ public Driver() { squarePen = new DrawingGizmo(); squarePen.dontDraw(); squarePen.moveBy(60); squarePen.turnBy(-90); squarePen.moveBy(60); squarePen.dontDraw(); squarePen.turnBy(180); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); squarePen.turnBy(90); squarePen.moveBy(120); // 2) Draw tic-tac-toe lines. // 3) Draw 4-corners opening out. } meaningful identifiers regular indentation Programming with Style

14 © 2006 Pearson Addison-Wesley. All rights reserved 2.5.14 Why bother??????? A Few Style Guidelines 2.Indent the bodies/clauses from their enclosing brackets/braces. (e.g. instance variable declarations and Driver code.) 3.An indentation distance of three or four characters is good. 1.Matching items should be aligned on the left. (e.g. public class … } ) 4.Place assertions (class invariants, pre- and post-conditions) immediately before their class or method. Use /** convention for pre/postconditions. 5.Pick identifiers that are meaningful (to you and others). 6.Variable names should be nouns or noun phrases. 7.Capitalize the first letter of each word in an identifier, excepting possibly the first. 8.Capitalize the first letter of the name of a class. Names of instance variables and methods should begin with a small letter. 9.Blank lines can be included to separate code, but should be used sparingly. 10.Assertions are a good use of comments. Minimize other comments.


Download ppt "Lecture 2.5 Top Down Design. © 2006 Pearson Addison-Wesley. All rights reserved 2.5.2 Therefore, problems must often be broken into small parts. Then."

Similar presentations


Ads by Google