Can someone tell me line by line, with comments, what the below Java program is doing? I know it is a pain but it helps me learn. Please and Thank you Source code: //NonTailRecursiveFactorial Class Body class NonTailRecursiveFactorial { public static long factorial(int n) { if (n == 0) { return 1; } return n * factorial(n - 1); } public static void main(String[] args) { for (int i = 0; i <= 10; i++) { System.out.println(i + "! = " + factorial(i)); } } } //TailRecursiveFactorial class body class TailRecursiveFactorial { public static long factorial(int n) { return factorial(n, 1); } private static long factorial(int n, long accumulator) { if (n == 0) { return accumulator; } return factorial(n - 1, accumulator * n); } public static void main(String[] args) { for (int i = 0; i <= 10; i++) { System.out.println(i + "! = " + factorial(i)); } } } //Test class named as FactorialTest public class FactorialTest { public static void main(String[] args) { testNonTailRecursiveFactorial(); testTailRecursiveFactorial(); } public static void testNonTailRecursiveFactorial() { System.out.println("Testing Non-Tail Recursive Factorial:"); for (int i = 0; i <= 10; i++) { long result = NonTailRecursiveFactorial.factorial(i); long expected = calculateFactorial(i); System.out.println(i + "! = " + result + " (Expected: " + expected + ")"); assert result == expected : "Test failed!"; } } public static void testTailRecursiveFactorial() { System.out.println("\nTesting Tail Recursive Factorial:"); for (int i = 0; i <= 10; i++) { long result = TailRecursiveFactorial.factorial(i); long expected = calculateFactorial(i); System.out.println(i + "! = " + result + " (Expected: " + expected + ")"); assert result == expected : "Test failed!"; } } public static long calculateFactorial(int n) { if (n == 0) { return 1; } long factorial = 1; for (int i = 1; i <= n; i++) { factorial *= i; } return factorial; } }
Can someone tell me line by line, with comments, what the below Java program is doing? I know it is a pain but it helps me learn.
Please and Thank you
Source code:
//NonTailRecursiveFactorial Class Body
class NonTailRecursiveFactorial {
public static long factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
}
//TailRecursiveFactorial class body
class TailRecursiveFactorial {
public static long factorial(int n) {
return factorial(n, 1);
}
private static long factorial(int n, long accumulator) {
if (n == 0) {
return accumulator;
}
return factorial(n - 1, accumulator * n);
}
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
}
//Test class named as FactorialTest
public class FactorialTest {
public static void main(String[] args) {
testNonTailRecursiveFactorial();
testTailRecursiveFactorial();
}
public static void testNonTailRecursiveFactorial() {
System.out.println("Testing Non-Tail Recursive Factorial:");
for (int i = 0; i <= 10; i++) {
long result = NonTailRecursiveFactorial.factorial(i);
long expected = calculateFactorial(i);
System.out.println(i + "! = " + result + " (Expected: " + expected + ")");
assert result == expected : "Test failed!";
}
}
public static void testTailRecursiveFactorial() {
System.out.println("\nTesting Tail Recursive Factorial:");
for (int i = 0; i <= 10; i++) {
long result = TailRecursiveFactorial.factorial(i);
long expected = calculateFactorial(i);
System.out.println(i + "! = " + result + " (Expected: " + expected + ")");
assert result == expected : "Test failed!";
}
}
public static long calculateFactorial(int n) {
if (n == 0) {
return 1;
}
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}
//NonTailRecursiveFactorial Class Body
class NonTailRecursiveFactorial {
public static long factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
}
//TailRecursiveFactorial class body
class TailRecursiveFactorial {
public static long factorial(int n) {
return factorial(n, 1);
}
private static long factorial(int n, long accumulator) {
if (n == 0) {
return accumulator;
}
return factorial(n - 1, accumulator * n);
}
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
}
//Test class named as FactorialTest
public class FactorialTest {
public static void main(String[] args) {
testNonTailRecursiveFactorial();
testTailRecursiveFactorial();
}
public static void testNonTailRecursiveFactorial() {
System.out.println("Testing Non-Tail Recursive Factorial:");
for (int i = 0; i <= 10; i++) {
long result = NonTailRecursiveFactorial.factorial(i);
long expected = calculateFactorial(i);
System.out.println(i + "! = " + result + " (Expected: " + expected + ")");
assert result == expected : "Test failed!";
}
}
public static void testTailRecursiveFactorial() {
System.out.println("\nTesting Tail Recursive Factorial:");
for (int i = 0; i <= 10; i++) {
long result = TailRecursiveFactorial.factorial(i);
long expected = calculateFactorial(i);
System.out.println(i + "! = " + result + " (Expected: " + expected + ")");
assert result == expected : "Test failed!";
}
}
public static long calculateFactorial(int n) {
if (n == 0) {
return 1;
}
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}
Step by step
Solved in 4 steps with 5 images
Can you also analyze the
Please and Thank You