From bfe7898011ce7e38892cc121cac4ba2ce270aee5 Mon Sep 17 00:00:00 2001 From: amin Date: Mon, 24 Jun 2024 15:50:14 +0100 Subject: [PATCH] docs: adding subject and main --- .../factorial-master/ExerciseRunner.java | 15 ++++ .../checkpoints/factorial-master/README.md | 73 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 subjects/java/checkpoints/factorial-master/ExerciseRunner.java create mode 100644 subjects/java/checkpoints/factorial-master/README.md diff --git a/subjects/java/checkpoints/factorial-master/ExerciseRunner.java b/subjects/java/checkpoints/factorial-master/ExerciseRunner.java new file mode 100644 index 000000000..44388c498 --- /dev/null +++ b/subjects/java/checkpoints/factorial-master/ExerciseRunner.java @@ -0,0 +1,15 @@ +public class ExerciseRunner { + public static void main(String[] args) { + Factorial iterativeFactorial = new IterativeFactorial(); + Factorial recursiveFactorial = new RecursiveFactorial(); + + // Test iterative factorial + int number = 5; + long iterativeResult = iterativeFactorial.calculate(number); + System.out.println("Iterative Factorial of " + number + " is: " + iterativeResult); // Expected output: 120 + + // Test recursive factorial + long recursiveResult = recursiveFactorial.calculate(number); + System.out.println("Recursive Factorial of " + number + " is: " + recursiveResult); // Expected output: 120 + } +} \ No newline at end of file diff --git a/subjects/java/checkpoints/factorial-master/README.md b/subjects/java/checkpoints/factorial-master/README.md new file mode 100644 index 000000000..9001159cf --- /dev/null +++ b/subjects/java/checkpoints/factorial-master/README.md @@ -0,0 +1,73 @@ +## Factorial Master + +### Instructions + +Create an abstract class `Factorial` that will serve as the base class for calculating the factorial of a number. This class should have an abstract method `calculate` that will be implemented by its child classes. + +Implement two child classes: + +- `IterativeFactorial` which implements the factorial calculation using an iterative approach. +- `RecursiveFactorial` which implements the factorial calculation using a recursive approach. + +> Note: The factorial of 0 is 1, according to the [convention](https://www.chilimath.com/lessons/intermediate-algebra/zero-factorial/) + +#### Formula for factorial calculation: + +n! = n × (n−1) × (n−2) × … × 1 + +And an example: +5! = 5 × 4 × 3 × 2 × 1 = 120 + +### Expected Abstract Class + +```java +public abstract class Factorial { + public abstract long calculate(int n); +} + +public class IterativeFactorial extends Factorial { + @Override + public long calculate(int n) { + // iterative factorial calculation + } +} + +public class RecursiveFactorial extends Factorial { + @Override + public long calculate(int n) { + // recursive factorial calculation + } +} +``` + +### Usage + +Here is a possible ExerciseRunner.java to test your classes: + +```java +public class ExerciseRunner { + public static void main(String[] args) { + Factorial iterativeFactorial = new IterativeFactorial(); + Factorial recursiveFactorial = new RecursiveFactorial(); + + // Test iterative factorial + int number = 5; + long iterativeResult = iterativeFactorial.calculate(number); + System.out.println("Iterative Factorial of " + number + " is: " + iterativeResult); // Expected output: 120 + + // Test recursive factorial + long recursiveResult = recursiveFactorial.calculate(number); + System.out.println("Recursive Factorial of " + number + " is: " + recursiveResult); // Expected output: 120 + } +} +``` + +### Expected Output + +```shell +$ javac *.java -d build +$ java -cp build ExerciseRunner +Iterative Factorial of 5 is: 120 +Recursive Factorial of 5 is: 120 +$ +```