The following incorrect code is supposed to find the integral average of the positive numbers and the integral average of the negative numbers in the array a with nElems elements.
public void avgPosAvgNeg (int [ ] a, int nElems)
{
int j;
int sumPos = 0; int sumNeg = 0; int nPos = 0; int nNeg = 0;
for (j = 0; j < nElems; j++)
{
if (a[j] > 0)
{
sumPos = sumPos + a[j];
nPos++;
}
else if (a[j] < 0)
{
sumNeg = sumNeg - a[j];
nNeg++;
}
}
System.out.println(" Average of positive numbers: “ + sumPos/nPos);
System.out.println(" Average of negative numbers: “ + sumNeg/nNeg);
}
Go through the four phases in the debugging process. [a] In the stabilization phase, list the minimal test cases that produce the error. [b] In the localization phase, identify the section of the code that causes the error. [c] In the correction phase, show the correct code that fixed the error. [d] In the verification phase, what test cases would you use to make sure the error is fixed, and no other errors were introduced?
[a]. Stabilization phase
Here after completing the entire code , when it is executed,
Here we took no of elements=6
Elements are:
5 -8 -9 6 2 -2
Result is shown as: Average of positive numbers: 4 ( decimal part missing)
Average of negative numbers: 6 (decimal and negative sign before digit are missing)
The following error is in the output

In this way , for no of elements=8
Elements are:
4 -2 -3 -5 6 3 2 -1
Result is shown as: Average of positive numbers: 3 ( decimal part missing)
Average of negative numbers: 2 (decimal part and negative sign before digit are missing)
[b].localization phase
public void avgPosAvgNeg (int [ ] a, int nElems)
{
int j;
int sumPos = 0; int sumNeg = 0; int nPos = 0; int nNeg = 0; // Error1
for (j = 0; j < nElems; j++)
{
if (a[j] > 0)
{
sumPos = sumPos + a[j];
nPos++;
}
else if (a[j] < 0)
{
sumNeg = sumNeg - a[j]; // Error2
nNeg++;
}
}
System.out.println(" Average of positive numbers: " + sumPos/nPos); // Error3
System.out.println(" Average of negative numbers: " + sumNeg/nNeg); //Error4
}
Error1: in place of int sumPos = 0; int sumNeg = 0;
It will be double sumPos = 0; double sumNeg = 0;
( double will help to calculate decimal value to generate final average)
Error2: in place of sumNeg = sumNeg - a[j];
It will be sumNeg = sumNeg + a[j];
( because in first case as a[j] is –ve so sumNeg will generate positive value always. Because minus of minus always gives positive value. So the code will be positive as per second case)
Error3 and Error4:
Average of positive numbers: " + sumPos/nPos
Average of negative numbers: " + sumNeg/nNeg
Will be
Average of positive numbers: " + Double.valueOf(sumPos/nPos)
Average of negative numbers: " + Double.valueOf(sumNeg/nNeg)
(Conversion in double the average will give perfect result, if it has decimal part)
[c]. Correction phase
import java.util.Scanner;
class avg
{
public void avgPosAvgNeg (int [ ] a, int nElems)
{
int j;
double sumPos = 0; double sumNeg = 0; int nPos = 0; int nNeg = 0;
for (j = 0; j < nElems; j++)
{
if (a[j] > 0)
{
sumPos = sumPos + a[j];
nPos++;
}
else if (a[j] < 0)
{
sumNeg = sumNeg + a[j];
nNeg++;
}
}
System.out.println(" Average of positive numbers: " + Double.valueOf(sumPos/nPos));
System.out.println(" Average of negative numbers: " + Double.valueOf(sumNeg/nNeg));
}
}
class inte
{
public static void main(String args[])
{
int n,i;
int a[]=new int[100];
avg av = new avg();
Scanner ob = new Scanner(System.in);
System.out.println("how many elements");
n = Integer.parseInt(ob.nextLine());
System.out.println("enter elements:");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(ob.nextLine());
}
av.avgPosAvgNeg ( a, n);
}
}
[d]. Verification phase

No of elements=6
5 -8 -9 6 2 -2
Output gives: for positive numbers, the average is : 4.333333333333333
for negative numbers , the average is : -6.333333333333333
No of elements=8
4 -2 -3 -5 6 3 2 -1
Output gives: for positive numbers, the average is : 3.75
for negative numbers , the average is : -2.75
Outputs for both the cases are absolutely right with respect to sign convention and appropriate result.
The following incorrect code is supposed to find the integral average of the positive numbers and...