|
|
|
|
|
|
|
The Problem-Solving Phase: The Algorithm Walk-Through |
|
|
|
|
|
|
|
|
To test at the problem-solving phase, we do a walk-through of the algorithm. For each module in the top-down design, we establish an assertion called a precondition and another called a postcondition. A precondition is an assertion that must be true before a module is executed in order for the module to execute correctly. A postcondition is an assertion that should be true after the module has executed, if it has done its job correctly. To test a module, we walk through the algorithmic steps to confirm that they produce the required postcondition given the stated precondition. |
|
|
|
 |
|
 |
|
|
Precondition An assertion that must be true before a module begins executing. |
|
|
|
 |
|
 |
|
|
Postcondition An assertion that should be true after a module has executed. |
|
|
|
|
|
|
|
|
Our lumberyard algorithm has four modules: the main module, Get Data, Calculate Lumber Amount, and Calculate Plywood Amount. Usually there is no precondition for a main module. Our main module's postcondition is that it outputs the correct result given the correct input. More specifically, the postcondition for the main module is |
|
|
|
|
|
|
|
|
the computer has read five input values (a letter and four integers). |
|
|
|
|
|
|
|
|
either the number of board feet ordered or the number of full sheets of plywood ordered has been calculated and displayed. |
|
|
|
|
|
|
|
|
Because Get Data is the first module executed in the algorithm and because it does not assume anything about the contents of the variables it is about to manipulate, it has no precondition. Its postcondition is that it has input a letter into code, and integer values into size1, size2, size3, and numOrdered. |
|
|
|
|
|
|
|
|
The precondition for module Calculate Lumber Amount is that code equals L, and that size1, size2, size3, and numOrdered contain integer values. Its postcondition is that the number of board feet ordered has been computed from those values and printed out. |
|
|
|
|
|
|
|
|
The precondition for module Calculate Plywood Amount is that code equals P, and that size1, size2, size3, and numOrdered contain integer values. Its postcondition is that the number of full sheets ordered has been computed and printed out. |
|
|
|
|
|
|
|
|
Below we summarize the module preconditions and postconditions in tabular form. In the table, we use AND with its usual meaning in an assertionthe logical AND operation. Also, a phrase like someVariable is as- |
|
|
|
|
|