We elaborate the systematic debugging approach by a concrete example implementing a stack. The program is given in Section 3 including the Stack class plus a main function providing an interactive console interface to play with an instance of Stack.
If you run the given main function in Section 3, and test it by entering a number of pushes and asking for the ‘sum’, you will see an in correct result. For example, Figure 1 shows the sum after two pushes. Hence, there might be an error in Stack class that has to be resolved. Since the code was compiled perfectly, syntax error is not plausible and the error has to be logical. We start debugging the logical error by proposing hypothesis 1.
Hypothesis 1. Since the sum function returns incorrect value, it might be implemented inaccurately.
Since hypothesis 1 is rejected, we have to bring up another hypothesis and evaluate it.
Hypothesis 2. Push does not insert the first item in the beginning of the array.
Hypothesis 2 is correct, now it has to be refined into a more precise hypothesis revealing the root of the problem and a way to fix it.
Hypothesis 3. The index showing the first available empty spot in increased before the item is inserted.
Based on hypothesis 3, we fixed the bug.