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

Create the JAVA Code: This exercise is intended to demonstrate recursive coding,

ID: 3586768 • Letter: C

Question

Create the JAVA Code:

This exercise is intended to demonstrate recursive coding, the Java Math class, and exception handling. You are to write a Java program that has at least two functions: main and recursionFactorial. main will be the entry point/driver and recursionFactorial will be a function that recursively computes the factorial of an integer parameter. There are some complications. A factorial is defined as follows where n >= 0. O! Is defined to be 0, 1! Is also defined as 1. int variables are 4 bytes, and the maximum value that can be stored in an int is 2,147,483,647. As you compute factorials for a range of increasing integers, the values become very large very quickly and exceed the maximum value. Oddly, in Java the basic arithmetic operations will simply overflow when that happens without any warning. A program in which that happens is allowed to continue executing with corrupted data. However, there is a static Java class, Math, which since the advent of Java 1.8 offers a solution to that problem. (You may have already used Math with such functions as Math.abs(), Math.power), and Math.sqrt0). The functions addExact) subtractExact0, multiplyExact) duplicate the functions their names imply, and in addition they throw exceptions if the results cause overflows. For instance, instead of multiplying one variable by another with regular operators you would use the Math function Math.multiplyExact(a, b) f the result of the multiplyExact operation is an overflow, it will throw an ArithmeticException exception. Your program should compute the factorials for the range of integers 1 -20 (int). You may hard-code this range. You should use the Math.multiplyExact0) function for the factorial multiplications. If while you are computing a factorial there is an overflow, handle the exception and discontinue that factorial, and then proceed with the next one. You should print a clearly labeled, formatted report of your results

Explanation / Answer

/*

Factorial of Numbers from 1 to 20 using Math.multiplyExact() method by catching the exception.

*/

public class PrintFactorial {

static int factorial(int num) {

int fact = -1;

try {

if (num == 0)

fact = 0;

else if (num == 1)

fact = 1;

else {

fact = Math.multiplyExact(num, factorial(num-1));

}

} catch(ArithmeticException ae) {

System.out.println("Integer Overflow");

}

return fact;

}

public static void main(String args[]){

System.out.println("Factorial of the Numbers from 1 to 20:");

System.out.println("Number Factorial Value");

for(int number=1; number<=20; number++){

System.out.print(number + " ");

int result = factorial(number);

if(result >= 0)

System.out.println(result);

}

}

}

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