Unformatted text preview:

University of Washington Computer Programming I Lecture 13 Functions and Design 2000 UW CSE L 1 Overview Design process Functional decomposition Top down vs bottom up Graphics primitives L 2 Drawing a House L 5 Drawing a Similar House L 6 Draw House Pseudo code draw house color ll x ll y num windows draw body as a colored rectangle draw roof as a colored triangle if num windows is one draw door draw window if num windows is two draw door draw window draw window L 7 Functional Decomposition Draw House Draw Body Draw Roof Draw Door Rectangle Triangle Rectangle Circle Draw Window Rectangle Line This is a calling tree or static call graph Each function is shown with an arrow down to each function called L 8 Functional Decomposition Draw House Draw Roof Draw Body Draw Door Draw Window Triangle Rectangle Circle Line Each function shown only once preferred L 9 Analysis to Design to Programming Analyze the problem Then design a big picture solution A functional decomposition shows how the pieces fit together Then design individual functions May depend on low level primitive functions available Final programming may be very detailed L 10 Top Down vs Bottom Up Sometimes designers start from the big picture Gradually work down to smaller pieces and then to fine details Called the top down approach Sometimes people start with small pieces Figure out how they can fit together pieces to solve ever larger and larger problems Called the bottom up approach L 11 Top Down or Bottom Up Which approach are we following with DrawHouse Answer Generally top down But we have to look ahead and know what low level functions will be available Eventually there will be graphics programming to do Fortunately most systems supply a library of graphics primitives L 12 Graphics Primitive Typical functions clearscreen draw circle rectangle line ellipse etc Typical parameters location color fill etc Requires a coordinate system Y a b 0 0 X L 13 Typical rectangle and line void rectangle int color int x1 int y1 int x2 int y2 void line int x1 int y1 int x2 int y2 x2 y2 x1 y1 x2 y2 x1 y1 L 14 Big Picture Again Draw House Draw Roof Draw Body Triangle Rectangle Draw Door Draw Window Circle Line Fill in the pieces one at a time L 15 Window Constants Our analysis of how to describe a window MID X WIN H MID Y WIN W L 16 Map Analysis to C Code Identify and declare constants Choose parameters Utilize primitives Get the picky details right too void draw window int x int y x y is the lower left corner of the window rectangle WHITE x y x WIN W y WIN H line x MID X y x MID X y WIN H line x y MID Y x WIN W y MID Y L 17 Keep Filling in Pieces Draw House Draw Roof Triangle Draw Body Rectangle Draw Door Circle Draw Window Line Analyze and code remaining functions Does the order matter Coding could be bottom up even if design was top down and vice versa If the design is good the functions can be implemented independently L 18 Draw House Gory Detail I void draw house int color int ll x int ll y int windows int roof ll x roof ll y Draw Body draw body color ll x ll y Draw Roof roof ll x ll x OVERHANG roof ll y ll y BODY HEIGHT draw roof color roof ll x roof ll y L 19 Draw House Gory Detail II Draw Door and Window s if windows 1 draw door ll x DOOR OFFSET 1 ll y draw window ll x WINDOW OFFSET 1 ll y WINDOW RAISE else L 20 Draw House Gory Detail II Draw Door and Window s if windows 1 draw door ll x DOOR OFFSET 1 ll y draw window ll x WINDOW OFFSET 1 ll y WINDOW RAISE else if windows 2 draw door ll x DOOR OFFSET 2 ll y draw window ll x WINDOW OFFSET 2A ll y WINDOW RAISE draw window ll x WINDOW OFFSET 2B ll y WINDOW RAISE L 21 Next Step A Neighborhood We could write 6 different functions Smarter call 1 function 6 times L 23 Summary of Functional Decomposition Look for common elements similarities Parameterize for special features differences Determine which functions will use others Draw a graph to show their relationships L 24


View Full Document

UW CSE 142 - Functions and Design

Loading Unlocking...
Login

Join to view Functions and Design and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions and Design and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?