Java Reference
In-Depth Information
* @param lowerLimit The lower limit of the integer range
* @param upperLimit The upper limit of the integer range
* @return The sum of all integers between lowerLimit (inclusive)
* and upperLimit (inclusive)
*/
public static int getRangeSum(int lowerLimit, int upperLimit) {
int sum = 0;
int counter = lowerLimit;
while(counter <= upperLimit) {
sum = sum + counter;
counter = counter + 1;
}
return sum;
}
It uses Javadoc standards to write a specification for a Java program that can be processed by the Javadoc tool
to generate HTML pages. In Java, the specification for a program element is placed between /** and */ immediately
before the element. The specification is meant for the users of the getRangeSum procedure. The Javadoc tool will
generate the specification for the getRangeSum procedure, as shown in Figure 1-4 .
Figure 1-4. The specification for the getRangeSum procedure
The above specification provides the description (the “what” part) of the getRangeSum procedure. It also
specifies two conditions, known as pre-conditions, that must be true when the procedure is called. The first pre-
condition is that the lower limit must be less than or equal to the upper limit. The second pre-condition is that the
value for lower and upper limits must be small enough so that the sum of all integers between them fits in the size
of the int data type. It specifies another condition that is called post-condition, which is specified in the “Returns”
clause. The post-condition holds as long as pre-conditions hold. The pre-conditions and post-conditions are like a
contract (or an agreement) between the program and its user. It states that as long as the user of the program makes
sure that the pre-condition holds true, the program guarantees that the post-condition will hold true. Note that the
specification never tells the user about how the program fulfils (implementation details) the post-condition. It only
tells “what” it is going to do rather than “how” it is going to do it. The user of the getRangeSum program, who has the
specification, need not look at the body of the getRangeSum procedure to figure out the logic that it uses. In other
 
Search WWH ::




Custom Search