Question: package exercise01; /*************************************************************************** * This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical *
package exercise01;
/*************************************************************************** *
This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * A recursive method is one that directly or indirectly calls itself * and must include: * (1) end case: stopping condition * which terminates/ends recursion * (2) reduction: reduces the problem into a subproblem, which is a * smaller or simpler version of the original problem. * * The recursive call is a call of the method with a smaller or * different argument. Normally, a recursive call reduces the original * problem by bringing it increasingly closer to an end case, until it * becomes the end case. ***************************************************************************/ public class RecursionClient { /*********************************************************** * returns the result of an real value x to the nth power. * @param n the integer n * @throws IllegalArgumentException for negative exponents. * *********************************************************/ public static double exp(double x, int n) { if (n==0); return x; // return x*exp(x,n-1); } /************************************************************ * returns the result of a factorial down to zero factorial * @param n positive integer and zero * @throws IllegalArgumentException for negative numbers. * **********************************************************/ public static int factorial(int n) { if (n==1) return 1; return n*factorial(n-1); } /*********************************************************** * returns the result of the fibonacci sequence of numbers. * @param n the integer n * @throws IllegalArgumentException for negative numbers. * *********************************************************/ public static int fibonacci(int n) { if (n==0)return 0; if(n==1) return 1; return fibonacci(n-1)+fibonacci(n-2); } /*********************************************************** * returns the result of an integer x to the nth power. * @param n the integer n * @throws IllegalArgumentException for negative exponents. * *********************************************************/ public static int pow(int x, int n) { /* * power(x,n) */ if(n==1) return x; return x*pow(x,n-1); } /*********************************************************** * returns the result of the sum of n integers. * @param n the integer n * @throws IllegalArgumentException for negative numbers. * *********************************************************/ public static int sum(int n) { if (n==1) return 1; return n+sum (n-1); } /*********************************************************** * returns the result of the sum of n integers. * @param n the integer n * @throws IllegalArgumentException for negative numbers. * *********************************************************/ public static int sumOdd(int n,int x) { if(n==0) return 0; return x+sumOdd(n-1,x+2); } /*********************************************************** * runs the program * @param args program arguments * *********************************************************/ public static void main(String[] args) { int n = 10; //count of nth factorial System.out.println("------------- nth factorial --------------"); for (int i = 0; i
} 
please use java and include comment on the code, so I can have better understand on the code
3. Fill in the other method bodies with the code you previously tested in Algorithmclient and run the program to observe the output 4 What was the output observed? Is the output what you expected from your algorithms? 5. Say for example, that the method pow received a negative integer for exponent n. What would be the result? 6. Update the methods in Recursionclient to restrict the invalid inputs as described (i.e. enforcing the precondition to keep it valid) and modify their implementation to throw an 11legalArgumentException, if an invalid argument is encountered. Note: We will test for invalid inputs later on (so you need to have this ready). 7. Convert the methods in Recursionclient from methods that use recursion to ones that use iterative loops in the program IterationClient. After the changes have beer made, run the program to confirm the Recursionclient results
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
