< previous page page_157 next page >

Page 157
Implementing the Design
The product of top-down design is a hierarchical solution to a problem with multiple levels of abstraction. Figure 4-5 shows a top-down design for the ConePaint program in Chapter 3. This kind of solution forms the basis for the implementation phase of programming.
How do we translate a top-down design into a C++ program? If you look closely at Figure 4-5, you can see that the concrete steps (those that are shaded) can be assembled into a complete algorithm for solving the problem. The order in which they are assembled is determined by their position in the tree. We start at the top of the tree, at level 0, with the first step, Convert dimensions to feet. Because it is abstract, we must go to the next level, level 1. There we find a series of concrete steps that correspond to this step; this series of steps becomes the first part of our algorithm. Because the conversion process is now concrete, we can go back to level 0 and go on to the next step, finding the radius of the cone. This step is concrete; we can copy it directly into the algorithm. The last three steps at level 0 are abstract, so we work with each of them in order at level 1, making them concrete. Here's the resulting algorithm:
Set heightInFeet = heightInInches / 12
Set diameterInFeet = diameterInInches / 12
Set radius = diameterInFeet / 2
Set surfaceArea = pi 
* radius * sqrt(radius*radius + heightInFeet*heightInFeet)
Set redCost = surfaceArea 
* 0.10
Set blueCost = surfaceArea 
* 0.15
Set greenCost = surfaceArea 
* 0.18
Print surfaceArea
Print redCost
Print blueCost
Print greenCost
From this algorithm we can construct a table of the constants and variables required, and then write the declarations and executable statements of the program.
In practice, you write your design not as a tree diagram but as a series of modules grouped by levels of abstraction, as we've done below.
Main Module                    Level 0
Convert dimensions to feet
Set radius = diameterInFeet / 2
Compute surface area
Compute costs
Print results

 
< previous page page_157 next page >