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

I have a code, but I keep getting multiple errors and I\'m not sure why. Any hel

ID: 3628328 • Letter: I

Question

I have a code, but I keep getting multiple errors and I'm not sure why. Any help would be greatly appreciated! Lifesaver rating is guaranteed if you get it to work!

/**
* Write a method called sum that takes two int arrays as input parameters and
* returns a new int array that is the sum of the two arrays. For example, on
* input {1,2,3} and {4,5,6}, it should return {5,7,9}. In your main method,
* test the sum method on inputs {1,2,3}, {4,5,6} and print the result.
*/
public class ArraySum {
int[] sum;

public static int[] sum(int[] a, int[] b) {
sum = new int[Math.min(a.length, b.length)];
for (int i = 0; i < sum.length; i++){
sum [i] = a[i] + b[i];
}
return sum;
}

public static boolean testSum(int[] a, int[] b, int[] expected) {
int result = sum(a,b);
if (result == expected) {
System.out.println("The sum of the two arrays is" + result);
return true;
}
else{
System.out.println("The sum of the two arrays is " + result + " not" + expected);
return false;
}
}


public static void main(String[] args) {
int[] x = {1,2,3};
int[] y = {4,5,6};
int[] expected = {5,7,9};
testSum(x, y, expected);
}

}

Explanation / Answer

/** * Write a method called sum that takes two int arrays as input parameters and * returns a new int array that is the sum of the two arrays. For example, on * input {1,2,3} and {4,5,6}, it should return {5,7,9}. In your main method, * test the sum method on inputs {1,2,3}, {4,5,6} and print the result. */ public class ArraySum { public static int[] sum(int[] a, int[] b) { int[] sum; sum = new int[Math.min(a.length, b.length)]; for (int i = 0; i