I am working on a lab where we needed to locate an error in this method sumOfPositives. My response is that the method does not account for double types. We are then to write an assertion at the end of the test method that conforms to the initial change made. I can't seem to figure this out. My answer was to write this assertion: assertEquals(1.5, Util.sumOfPositives(new double[] {0,1,0.5}));
but it is not working correctly. What am I doing wrong?
Initial code:
public static int sumOfPositives(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
@Test
public void testSumOfPositives() {
assertEquals(3, Util.sumOfPositives(new int[] { 1, 1, 1 }));
assertEquals(7, Util.sumOfPositives(new int[] { 1, 2, 4 }));
assertEquals(9, Util.sumOfPositives(new int[] { 0, 0, 3, 6 }));
}
}
My changes:
public static double sumOfPositives(double[] arr) {
double sum = 0;
for (double i : arr) {
sum += i;
}
return sum;
}
@Test
public void testSumOfPositives() {
assertEquals(3, Util.sumOfPositives(new int[] { 1, 1, 1 }));
assertEquals(7, Util.sumOfPositives(new int[] { 1, 2, 4 }));
assertEquals(9, Util.sumOfPositives(new int[] { 0, 0, 3, 6 }));
assertEquals(1.5, Util.sumOfPositives(new double[] {0,1,0.5}));
}
Aucun commentaire:
Enregistrer un commentaire