|
| 1 | +import java.io.File; |
| 2 | +import java.io.FileWriter; |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +/** |
| 8 | +* Program that takes numbers from an input file. |
| 9 | +* For each number, it will evaluate the factorial of the number, |
| 10 | +* using a recursive function. |
| 11 | +* The results will be written onto an output file. |
| 12 | +* |
| 13 | +* @author Atri Sarker |
| 14 | +* @version 1.0 |
| 15 | +* @since 2025-11-17 |
| 16 | +*/ |
| 17 | +public final class Factorial { |
| 18 | + /** |
| 19 | + * Private constructor to satisfy style checker. |
| 20 | + * @exception IllegalStateException for the utility class. |
| 21 | + * @see IllegalStateException |
| 22 | + */ |
| 23 | + private Factorial() { |
| 24 | + // Prevents illegal states. |
| 25 | + throw new IllegalStateException("Utility class."); |
| 26 | + } |
| 27 | + /** |
| 28 | + * Recursive function that calculates the factorial of a number. |
| 29 | + * @param n The number to take the factorial of. |
| 30 | + * @return The factorial of n. |
| 31 | + */ |
| 32 | + public static int factorial(final int n) { |
| 33 | + // BASE CASE [0! = 1, 1! = 1] |
| 34 | + if (n <= 1) { |
| 35 | + return 1; |
| 36 | + } else { |
| 37 | + // RECURSIVE CALL |
| 38 | + return n * factorial(n - 1); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Entrypoint of the program. |
| 44 | + * @param args For command line arguments. |
| 45 | + */ |
| 46 | + public static void main(final String[] args) { |
| 47 | + // First argument is the path to the input file. |
| 48 | + final String inputFilePath = args[0]; |
| 49 | + // Second argument is the path to the output file. |
| 50 | + final String outputFilePath = args[1]; |
| 51 | + // Print arguments |
| 52 | + System.out.println("Input file: " + inputFilePath); |
| 53 | + System.out.println("Output file: " + outputFilePath); |
| 54 | + try { |
| 55 | + // Access the input file and create a File object. |
| 56 | + File inputFile = new File(inputFilePath); |
| 57 | + // Access the output file and create a File object. |
| 58 | + File outputFile = new File(outputFilePath); |
| 59 | + // Scanner that will read the File Object. |
| 60 | + Scanner fileReader = new Scanner(inputFile); |
| 61 | + // Create list to store all the ints |
| 62 | + ArrayList<Integer> listOfInts = new ArrayList<>(); |
| 63 | + // Loop through all available lines |
| 64 | + while (fileReader.hasNextLine()) { |
| 65 | + // Add the line to the list |
| 66 | + // [As an integer] |
| 67 | + try { |
| 68 | + // Convert line to an integer |
| 69 | + int num = Integer.parseInt(fileReader.nextLine()); |
| 70 | + // Add the number to the list |
| 71 | + listOfInts.add(num); |
| 72 | + } catch (NumberFormatException error) { |
| 73 | + // If the line can't be converted to an integer, |
| 74 | + // the program just ignores the line and continues. |
| 75 | + continue; |
| 76 | + } |
| 77 | + } |
| 78 | + // Close the file reader |
| 79 | + fileReader.close(); |
| 80 | + // Convert the list to an array |
| 81 | + int[] arrOfInts = new int[listOfInts.size()]; |
| 82 | + for (int index = 0; index < listOfInts.size(); index++) { |
| 83 | + arrOfInts[index] = listOfInts.get(index); |
| 84 | + } |
| 85 | + // String to hold all output |
| 86 | + String output = ""; |
| 87 | + // Go through every number and add the factorial to the output string |
| 88 | + for (int num : arrOfInts) { |
| 89 | + // Get the factorial |
| 90 | + final int result = factorial(num); |
| 91 | + // Add it to the output |
| 92 | + output += Integer.toString(result); |
| 93 | + // Print transformation |
| 94 | + System.out.print(num); |
| 95 | + System.out.print("! -> "); |
| 96 | + System.out.println(result); |
| 97 | + // Add a newline |
| 98 | + output += "\n"; |
| 99 | + } |
| 100 | + // Write output to output file |
| 101 | + try { |
| 102 | + // Create a FileWriter object to write to the file |
| 103 | + FileWriter writer = new FileWriter(outputFile); |
| 104 | + // Write the output to the file |
| 105 | + writer.write(output); |
| 106 | + // Close the writer |
| 107 | + writer.close(); |
| 108 | + } catch (IOException error) { |
| 109 | + System.out.println(error); |
| 110 | + } |
| 111 | + } catch (IOException error) { |
| 112 | + System.out.println(error); |
| 113 | + } |
| 114 | + // Completion message |
| 115 | + System.out.println("DONE!"); |
| 116 | + } |
| 117 | +} |
0 commit comments