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

You are to write a class called MinilabRecursion (be exact on the spelling). D E

ID: 3822935 • Letter: Y

Question

You are to write a class called MinilabRecursion (be exact on the spelling).

D Extra credit opportunity C ES O Type here to search MinilabRecurs You are to write a class called MinilabRecursion (be exact on the spelling). In the class, you will write 3 public methods (described below). The problem: )(2n Given the following summation formula Note that the summation on the left side of the equal sign really means: 12 22 32 42 52 m which is the same thing as ne (n- (n- 32 22 +12 If you are given a value of n and told to find the summation shown above, there would be three different ways to look at it and therefore solve it. You are to write three static methods that implement the following public methods: 1) A stati method called answerFormula(int n. which returns an integer that is the summation. This method should just use the formula on the right of the equal sign to calculate it. 2) A statie method called answerLoop(int n), which returns an integer that is the summation. This method should use aloop to implement the summation that is shown on the left of the equal sign. 3) A stati method called answerRecurse int n), which returns an integer that is the summation. This method should implement it recursively. To understand and do this, you must see where therecursion takes place If n 5 (for example) then: 2 +3 5 But that is the same thing as: 5 So the summation from 1 to 5 can be described in terms of the summation from 1 to 4. The summation from 1 to 4 can be described in terms of the summation from 1 to 3, etc. So the recursive definition of this summation um of the first n term where S(n) means ctivate Windows o Settings to activate Windows. 1:21 PM 4/21/2017

Explanation / Answer


public class MinilabRecursion {

// Default constructor not needed though.
public MinilabRecursion()
{
  
}
  
/**
* Returns sum of square of number from 1 to n using formula
*/
public static int answerForumula(int n)
{
if (n < 1)
{
System.out.println("Invalid number;");
return 0;
}
  
return n*(n+1)*(2*n+1)/6;
}
  
/**
* Returns sum of square of number from 1 to n using loop
*/
public static int answerLoop(int n)
{
if (n < 1)
{
System.out.println("Invalid number;");
return 0;
}
  
int squareSum = 0;
for(int i = 1; i <= n; i ++)
{
squareSum += i*i;
}
return squareSum;
}
  
/**
* Returns sum of square of number from 1 to n using recursion
*/
public static int answerRecurse(int n)
{
if (n < 1)
{
System.out.println("Invalid number;");
return 0;
}
  
if (n == 1)
{
return 1;
}
return n*n + answerRecurse(n-1);
  
}
}

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