Question: Please explain the following Java code line-by-line: import java.util.Scanner; class Factorial { public int factorial(int n) { int result = 1; for (int i =

Please explain the following Java code line-by-line:

import java.util.Scanner; class Factorial { public int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } } class Fibonacci { public int fibonacci(int n) { if (n <= 1) { return n; } int a = 0, b = 1, c; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } } class Contains { public boolean contains(int[] a, int x) { for (int i = 0; i < a.length; i++) { if (a[i] == x) { return true; } } return false; } } class Maximum { public int maximum(int[] a) { int max = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] > max) { max = a[i]; } } return max; } } class CountOccurrences { public int countOccurrences(int[] a, int x) { int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] == x) { count++; } } return count; } } class Reverse { public int[] reverse(int[] a) { int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) { result[i] = a[a.length - 1 - i]; } return result; } } class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Factorial factorial = new Factorial(); Fibonacci fibonacci = new Fibonacci(); Contains contains = new Contains(); Maximum maximum = new Maximum(); CountOccurrences countOccurrences = new CountOccurrences(); Reverse reverse = new Reverse(); boolean run = true; while (run) { System.out.println("Programs Available:"); System.out.println("1. Factorial"); System.out.println("2. Fibonacci Series"); System.out.println("3. Contains"); System.out.println("4. Maximum"); System.out.println("5. Count Occurrences"); System.out.println("6. Reverse"); System.out.print("Enter your choice: "); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("Enter a number: "); int n1 = scanner.nextInt(); System.out.println("Factorial of " + n1 + " is " + factorial.factorial(n1)); break; case 2: System.out.print("Enter a number: "); int n2 = scanner.nextInt(); System.out.println("Fibonacci of " + n2 + " is " + fibonacci.fibonacci(n2)); break; case 3: System.out.print("Enter the size of the array: "); int n3 = scanner.nextInt(); int[] a3 = new int[n3]; System.out.println("Enter the elements of the array: "); for (int i = 0; i < n3; i++) { a3[i] = scanner.nextInt(); } System.out.print("Enter the element to be searched: "); int x3 = scanner.nextInt(); System.out.println("The array " + (contains.contains(a3, x3) ? "contains" : "does not contain") + " " + x3); break; case 4: System.out.print("Enter the size of the array: "); int n4 = scanner.nextInt(); int[] a4 = new int[n4]; System.out.println("Enter the elements of the array: "); for (int i = 0; i < n4; i++) { a4[i] = scanner.nextInt(); } System.out.println("The maximum element in the array is " + maximum.maximum(a4)); break; case 5: System.out.print("Enter the size of the array: "); int n5 = scanner.nextInt(); int[] a5 = new int[n5]; System.out.println("Enter the elements of the array: "); for (int i = 0; i < n5; i++) { a5[i] = scanner.nextInt(); } System.out.print("Enter the element to be searched: "); int x5 = scanner.nextInt(); System.out.println("The element " + x5 + " occurs " + countOccurrences.countOccurrences(a5, x5) + " times in the array"); break; case 6: System.out.print("Enter the size of the array: "); int n6 = scanner.nextInt(); int[] a6 = new int[n6]; System.out.println("Enter the elements of the array: "); for (int i = 0; i < n6; i++) { a6[i] = scanner.nextInt(); } int[] reversed = reverse.reverse(a6); System.out.println("The reversed array is: "); for (int i = 0; i < n6; i++) { System.out.print(reversed[i] + " "); } System.out.println(); break; default: System.out.println("Invalid choice"); break; } System.out.print(" Do you want to continue? (y/n): "); char ch = scanner.next().charAt(0); // if statement for you want to continue or not if (ch == 'n' || ch == 'N') { run = false; } } scanner.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!