Write the method int[] sumOfArrays(int[] arr1, int[] arr2), which has two arrays
ID: 3632676 • Letter: W
Question
Write the method int[] sumOfArrays(int[] arr1, int[] arr2), which has two arrays as parameters and returns a new, different array. Each element of the new array should have the sum of the corresponding elements of the two parameter arrays(its first element should have the sum of the first elements of the two parameter arrays, etc.). For example, if the two parameter arrays had the values 2, 5, 6, and 1, 3, 1 respectively, the returned array should have the values 3, 8, 7. The parameter arrays should not be modified. If the parameter arrays do not have the same number of elements, the method should throw a Java library exception called IllegalArgumentException (it doesn't matter what its argument string is) instead of returning any value.Explanation / Answer
Hope this helps, please rate! //class named sumArray public class sumArray { //The sumOfArrays method used to sum 2 arrays public int[]sumOfArrays(int[]arr1,int[]arr2){ int[]sum; if(arr1.length!=arr2.length){ throw new IllegalArgumentException("Arrays are not the same size!"); } else{ sum = new int[arr1.length]; for(int i =0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.