Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Hi All - Please see the question below - I know that the sum method cycles

ID: 3879036 • Letter: J

Question

JAVA

Hi All - Please see the question below - I know that the sum method cycles through an array passed as a parameter. then it adds each value to the sum variable, which is then returned for use in comparison of the assertion tests. The only issue that I can find with the sequences is if the values passed in the array does not meet the value the sum should be equal to in the array.

What does the below method do? see the three test cases below. there is an important sequence not covered in the test cases, what is it? Give an example

public static int sum(int[] arr) {
       int sum = 0;
       for (int i = 0; i < arr.length; i++) {
           sum += arr[i];
           System.out.println(Arrays.toString(arr));
           System.out.println(sum);
       }

       return sum;

@Test
   public void testSumOfPositives() {
       assertEquals(3, Util.sumOfPositives(new int[] { 1, 2,0,}));
       assertEquals(7, Util.sumOfPositives(new int[] { 1, 2, 4 }));
       assertEquals(9, Util.sumOfPositives(new int[] { 0, 0, 3, 6 }));
   }
}

Explanation / Answer

Important Cases that are not covered is the Negative cases like :-

assertEquals(0, Util.sumOfPositives(new int[] { 0, 0, -3, -6 })); :
< Our method will give as -9 but the answer is 0 >
assertEquals(4, Util.sumOfPositives(new int[] { 0, 0, -3, 4 }));
<Our method will give as -3 +4 = 1 but the answer is 4 >

Since it is the sum of positives so we should get sum of only positive numbers and not negative numbers also

Thanks, let me know if there is any concern. PLEASE RATE and comment if you have any doubts/concerns