Question: I need help with an assignment, I have to change a given method to a generic method that is supposed to be a recursive as

I need help with an assignment, I have to change a given method to a generic method that is supposed to be a recursive as well. The method is supposed to return a sum from the given parameters.

  • Write a recursive method to sum the values in an integer array (the sumOfArray() )
    • In addition, make it to be a generic method

The given code is this: /** * Recursive method for generating sum of values in array * @param arr array of Integers * @param num index of array to sum all previous index values (including num) * @return sum of array values */ public static int sumOfArray(Integer[] arr, int num) { // Implement your code here return 0; } ---------------- I've tried doing this: public static > int sumOfArray(T[] arr, int num) { int sum = 0; //int add = sumOfArray(arr, arr.length - 1); for(T arrays : arr) if(num == 0) { return 0; } else if(num != 0) { sum = arrays + sumOfArray(arr, num - 1); } return sum; } But sum = arrays + sumOfArray(arr, num - 1); gets an error and Im lost on what to do. I did do the method before trying to make it into a generic method and this is how it was: public static int sumOfArray(Integer[] arr, int num) { if(n == 0) return arr[num]; else return arr[num]+ sumOfArray(arr,num - 1); } How would I be able to change it? Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!