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

Similarly to sumAll, squareSum will take in one parameter: max and figures out t

ID: 3529927 • Letter: S

Question

Similarly to sumAll, squareSum will take in one parameter: max and figures out the sum of all the squares starting 1 and up to including max2. For example if max is 5 then this method returns 55 by doing 1+4+9+16+25. You will need to implement both versions of the method just like sumAll. One version only takes one parameter and the other version takes an array as an additional argument. Declare the methods as appropriate in the space given in the file Lab21_7.java. Uncomment the invocation of squareSum() methods inside main. Output should be similar to lab 6 except there should be a pair of Called and Return statements as sumAll sample output. public class Lab21_7 { public static void init(long[] arr) { // Initialize all the entries of the array to 0 for (int i = 0; i < arr.length; i++) { arr[i] = 0; } } public static long sumAll(int max) { // sumAll basic version System.out.println("Called sumAll(" + max + ")"); long result; if (max <= 1) result = 1; else result = max + sumAll(max-1); System.out.println("Return sumAll(" + max + ") = " + result); return result; } public static long sumAll(long[] arr, int max) { // Simple Recursion with array System.out.println("Called sumAll(" + max + ")"); if (max <= 1) arr[max] = 1; else if (arr[max] == 0) arr[max] = max + sumAll(arr, max - 1); System.out.println("Return sumAll(" + max + ") = " + arr[max]); return arr[max]; } // Fill-in the appropriate methods to complete the lab public static void main(String[] args) { final int MAXSIZE = 100; long[] arr = new long[MAXSIZE]; System.out.println("sumAll output for 5 is " + sumAll(5)); System.out.println("sumAll output for 10 is " + sumAll(10)); System.out.println("sumAll output for 20 is " + sumAll(20)); System.out.println("sumAll output for 15 is " + sumAll(15)); System.out.println(); init(arr); System.out.println("sumAll output for 5 is " + sumAll(arr, 5)); System.out.println("sumAll output for 10 is " + sumAll(arr, 10)); System.out.println("sumAll output for 20 is " + sumAll(arr, 20)); System.out.println("sumAll output for 15 is " + sumAll(arr, 15)); System.out.println(); /* System.out.println("squareSum output for 5 is " + squareSum(5)); System.out.println("squareSum output for 10 is " + squareSum(10)); System.out.println("squareSum output for 20 is " + squareSum(20)); System.out.println("squareSum output for 15 is " + squareSum(15)); System.out.println(); init(arr); System.out.println("squareSum output for 5 is " + squareSum(arr, 5)); System.out.println("squareSum output for 10 is " + squareSum(arr, 10)); System.out.println("squareSum output for 20 is " + squareSum(arr, 20)); System.out.println("squareSum output for 15 is " + squareSum(arr, 15)); System.out.println(); System.out.println("Fibonacci Number 5 is " + fib(5)); System.out.println("Fibonacci Number 10 is " + fib(10)); System.out.println("Fibonacci Number 20 is " + fib(20)); System.out.println("Fibonacci Number 15 is " + fib(15)); System.out.println(); init(arr); System.out.println("Fibonacci Number 5 is " + fib(arr, 5)); System.out.println("Fibonacci Number 10 is " + fib(arr, 10)); System.out.println("Fibonacci Number 20 is " + fib(arr, 20)); System.out.println("Fibonacci Number 15 is " + fib(arr, 15)); System.out.println(); System.out.println("Factorial of 5 is " + factorial(5)); System.out.println("Factorial of 10 is " + factorial(10)); System.out.println("Factorial of 20 is " + factorial(20)); System.out.println("Factorial of 15 is " + factorial(15)); System.out.println(); init(arr); System.out.println("Factorial of 5 is " + factorial(arr, 5)); System.out.println("Factorial of 10 is " + factorial(arr, 10)); System.out.println("Factorial of 20 is " + factorial(arr, 20)); System.out.println("Factorial of 15 is " + factorial(arr, 15)); */ } }

Explanation / Answer

Similarly to sumAll, squareSum will take in one parameter: max and figures out the sum of all the squares starting 1 and up to including max2. For example if max is 5 then this method returns 55 by doing 1+4+9+16+25. You will need to implement both versions of the method just like sumAll. One version only takes one parameter and the other version takes an array as an additional argument. Declare the methods as appropriate in the space given in the file Lab21_7.java.


public static int squareSum(int max){

if(max<=1)

return 1;

for(int i=1;i<=max;i++){

sum+= i*i;

}

return sum;

}


//I dont under stand what the array of addiontal argument stands for. If you can clarify, I can write that function too.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote