CS 1063 Intro. to Programming
Loop Patch Example


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 Java 1.5 version of user input).

NOTE: INCORRECT PROGRAM


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:


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)

Here is the new run of this program:


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)

Here is the new run of this program:


Great! The program is working again. The point is that we have compounded patch upon patch until the simple program is a real mess (what is called a kludge in engineering and computer science, a horrible mess that nevertheless works).

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. If another programmer working with this code were to discover the "mistake" of initializing sum to 1, and put in a change to initialize it to 0, this "correction" would introduce a new bug because of the strange patch of subtracting an extra 1 from sum at the end.


Here are three possible good versions of this program. All three of these produce the correct answers and all three involve straightforward logic with no patches, weird initialization, or "corrections" at the end):

FIRST GOOD VERSION OF PROGRAM (Uses priming read. Changes partly commented in dark red.)


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


Revision date: 2005-11-18. (Please use ISO 8601, the International Standard.)