Java Reference
In-Depth Information
Clearly, a sentinel value must be chosen that cannot be confused with an acceptable
input value. Grades on a quiz are nonnegative integers, so -1 is an acceptable sentinel value
for this problem. Thus, a run of the class-average program might process a stream of inputs
such as 95, 96, 75, 74, 89 and -1. The program would then compute and print the class
average for the grades 95, 96, 75, 74 and 89; since -1 is the sentinel value, it should not
enter into the averaging calculation.
Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement:
The Top and First Refinement
We approach this class-average program with a technique called top-down, stepwise re-
finement , which is essential to the development of well-structured programs. We begin
with a pseudocode representation of the top —a single statement that conveys the overall
function of the program:
Determine the class average for the quiz
The top is, in effect, a complete representation of a program. Unfortunately, the top rarely
conveys sufficient detail from which to write a Java program. So we now begin the refine-
ment process. We divide the top into a series of smaller tasks and list these in the order in
which they'll be performed. This results in the following first refinement :
Initialize variables
Input, sum and count the quiz grades
Calculate and print the class average
This refinement uses only the sequence structure —the steps listed should execute in order,
one after the other.
Software Engineering Observation 4.3
Each refinement, as well as the top itself, is a complete specification of the algorithm—
only the level of detail varies.
Software Engineering Observation 4.4
Many programs can be divided logically into three phases: an initialization phase that
initializes the variables; a processing phase that inputs data values and adjusts program
variables accordingly; and a termination phase that calculates and outputs the final results.
Proceeding to the Second Refinement
The preceding Software Engineering Observation is often all you need for the first refine-
ment in the top-down process. To proceed to the next level of refinement—that is, the
second refinement —we commit to specific variables. In this example, we need a running
total of the numbers, a count of how many numbers have been processed, a variable to
receive the value of each grade as it's input by the user and a variable to hold the calculated
average. The pseudocode statement
Initialize variables
can be refined as follows:
Initialize total to zero
Initialize counter to zero
 
Search WWH ::




Custom Search