Java Reference
In-Depth Information
// Get coffee in the morning
if ( coffee not made ) {
// Make coffee
if ( real coffee desired ) {
brewCoffee();
} else {
makeInstant();
}
}
Pour coffee into cup.
/** Brew coffee. */
public void brewCoffee() { }
/** Make instant coffee. */
public void makeInstant() { }
Notice that we write the specification and header of the new procedures, but we
leave their bodies empty. If one of them were a function, we would write a return
statement that produced some value of the right type, simply so that we could
compile the program, if we were writing and testing the program incrementally.
We can now work on either procedure; we decide to implement the body of
procedure brewCoffee . To brew coffee, we grind coffee, put the coffee in the fil-
ter, put the filter in the coffee maker, add water, and turn the coffee maker on:
/** Brew coffee */
public void brewCoffee() {
Grind coffee.
Put coffee in filter.
Put filter in coffee maker.
Add water.
Turn coffee maker on.
}
We stop the stepwise refinement of this program now, for the idea should be
clear. (Much more detail could be added —e.g. how do we boil water?)
2.5.2
A summary of stepwise refinement
We summarize the ideas of stepwise refinement, or top-down programming. The
development of the program consists of a series of steps.
1. Each step consists of replacing a statement that says what to do by a
sequence of one or more statements that describe how to do it. The
replacement is called the implementation , or refinement, of the statement.
 
Search WWH ::




Custom Search