CS 1713 Introduction to Computer Science
Simple Example of Patching a Loop

For more material about loops, see Lecture 4 and Lecture 5.

Extended Loop Patching Example:

In the examples below, various deliberate errors are made initially to illustrate the process of programming.

The examples are shown just as simple loops inside a main function, but at the end the task of the loop is reformulated to take the form of a object-oriented example.

Consider the following problem statement:

You are to write a Java program that will read integer exam scores one at a time until a -1 signals the end of the input. The first try at a program is the following (which uses the GetNext class for user input):

NOTE: INCORRECT PROGRAM


Copy in the file GetNext.java for use in reading ints. When ExamScores.java is compiled, there is the complaint that the variable score inside the while condition may not be initialized. (This is a artificial problem. The only reason to initialize score is to get past the first while test. Later score will always have the last value read.) So we replace int score; with int score = 0;. (Such initialization is called a patch.) Then recompile. This time the compile is successful. Run this program with input data:
90 100 80 -1
so that the average should be 90

When the program is run with the given input, the result is:


count: 4, sum: 269
average: 67.25 

Well, this isn't right! The average is not even close! Thinking about it, we see that the program is counting the final -1 and also adding it into the sum. The result is that the count is one too big and the sum is one too small. So we just initialize count to -1 and sum to 1 to compensate. (This is a much worse pair of patches.) This gives the following, with the corresponding run:

NOTE: FLAWED PROGRAM (flaws partly marked in red)


This is correct. We've successfully gotten the program working.

The next day our supervisor says that users are entering various negative numbers to terminate the input, rather than just -1, and they are getting the wrong answers. We try to insist that the users be made to use just -1, but eventually we agree to rewrite the program so that any negative number will terminate the input. Eventually, we decide to alter the final value of score so that the answer will be correct. Just after the while loop, we add the statement:

which should compensate for the extra negative score added in. When we compile and run this, the result is even further off, so we try:

This is only off by 1, so we subtract a 1 at the end to get the right answer. (This amounts to a really gross patch, often called a kludge.)

We also waste a run till we realize that the while condition needs to be while (score >= 0). The final version of the program now looks like:

NOTE: TERRIBLY FLAWED PROGRAM (flaws partly marked in red)


Great! The program is working again. The point is that we have compounded patch upon patch until the simple program is a real mess (a kludge).

Notice that in the statement sum = sum - score - 1;, the final - 1 is only there because of the earlier patch to initialize sum to 1.


Here are three possible good versions of this program (all three produce the correct answers):

FIRST GOOD VERSION OF PROGRAM (Uses infinite loop. Changes partly commented in red.)


SECOND GOOD VERSION OF PROGRAM (Uses priming read. Changes partly commented in red.)
THIRD GOOD VERSION OF PROGRAM (Uses read and assign inside while-condition. Changes partly commented in red.)
Finally, here is this example converted into a more "object-oriented" form, with a class ExamSocres that does all the work:


Revision date: 2003-10-03. (Please use ISO 8601, the International Standard.)