diff options
author | braxtonhall | 2022-10-24 02:13:46 +0000 |
---|---|---|
committer | braxtonhall | 2022-10-24 02:13:46 +0000 |
commit | 46a659c983911b87b38b20cd4b28ab9176e4fdb3 (patch) | |
tree | a0feef04229b8a9cf19d4a14eb29f773c4f394ef | |
parent | f4e141bb52b93096642a81a35b28c59dcc8dcdb5 (diff) |
Add fib-java
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | entries/nritschel/fib-java/fib.iml | 13 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/CachedFibonacciNumberFactory.java | 13 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/FibonacciCalculator.java | 3 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/FibonacciCalculatorImpl.java | 17 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/FibonacciNumber.java | 19 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/FibonacciNumberFactory.java | 3 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/Main.java | 21 | ||||
-rw-r--r-- | entries/nritschel/fib-java/src/NaiveFibonacciNumberFactory.java | 6 |
9 files changed, 98 insertions, 0 deletions
@@ -31,6 +31,9 @@ For a submission to the upcoming "Reclaim your space" exhibition at [Hatch Art G ### [`Metroxe`](https://github.com/Metroxe) - [`html`](./entries/Metroxe/index.html) +### [`nritschel`](https://github.com/nritschel) +- [`fib-java`](./entries/nritschel/fib-java/src) + ### [`shayanh`](https://github.com/shayanh) - [`matrix`](./entries/shayanh/matrix.go) diff --git a/entries/nritschel/fib-java/fib.iml b/entries/nritschel/fib-java/fib.iml new file mode 100644 index 0000000..815e958 --- /dev/null +++ b/entries/nritschel/fib-java/fib.iml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="module-library"> + </orderEntry> + </component> +</module>
\ No newline at end of file diff --git a/entries/nritschel/fib-java/src/CachedFibonacciNumberFactory.java b/entries/nritschel/fib-java/src/CachedFibonacciNumberFactory.java new file mode 100644 index 0000000..5c404e8 --- /dev/null +++ b/entries/nritschel/fib-java/src/CachedFibonacciNumberFactory.java @@ -0,0 +1,13 @@ +import java.util.HashMap; + +public class CachedFibonacciNumberFactory implements FibonacciNumberFactory { + private final HashMap<Integer, FibonacciNumber> cachedNumbers = new HashMap<>(); + + @Override + public FibonacciNumber getFibonacciNumber(int num) { + if (!cachedNumbers.containsKey(num)) { + cachedNumbers.put(num, new FibonacciNumber(num)); + } + return cachedNumbers.get(num); + } +} diff --git a/entries/nritschel/fib-java/src/FibonacciCalculator.java b/entries/nritschel/fib-java/src/FibonacciCalculator.java new file mode 100644 index 0000000..8806c20 --- /dev/null +++ b/entries/nritschel/fib-java/src/FibonacciCalculator.java @@ -0,0 +1,3 @@ +public interface FibonacciCalculator { + public int calculateFibonacci(FibonacciNumber fib); +} diff --git a/entries/nritschel/fib-java/src/FibonacciCalculatorImpl.java b/entries/nritschel/fib-java/src/FibonacciCalculatorImpl.java new file mode 100644 index 0000000..7bb5ee9 --- /dev/null +++ b/entries/nritschel/fib-java/src/FibonacciCalculatorImpl.java @@ -0,0 +1,17 @@ +public class FibonacciCalculatorImpl implements FibonacciCalculator { + private final FibonacciNumberFactory factory; + + public FibonacciCalculatorImpl(FibonacciNumberFactory factory) { + this.factory = factory; + } + + @Override + public int calculateFibonacci(FibonacciNumber fib) { + if (fib.getNumber() <= 2) { + return 1; + } + else { + return factory.getFibonacciNumber(fib.getNumber() - 1).calculate(this) + factory.getFibonacciNumber(fib.getNumber() - 2).calculate(this); + } + } +} diff --git a/entries/nritschel/fib-java/src/FibonacciNumber.java b/entries/nritschel/fib-java/src/FibonacciNumber.java new file mode 100644 index 0000000..fc9381f --- /dev/null +++ b/entries/nritschel/fib-java/src/FibonacciNumber.java @@ -0,0 +1,19 @@ +public class FibonacciNumber { + private final int number; + private Integer result; + + public FibonacciNumber(int number) { + this.number = number; + } + + public int getNumber() { + return number; + } + + public int calculate(FibonacciCalculator calculator) { + if (result == null) { + result = calculator.calculateFibonacci(this); + } + return result; + } +} diff --git a/entries/nritschel/fib-java/src/FibonacciNumberFactory.java b/entries/nritschel/fib-java/src/FibonacciNumberFactory.java new file mode 100644 index 0000000..42ef6c2 --- /dev/null +++ b/entries/nritschel/fib-java/src/FibonacciNumberFactory.java @@ -0,0 +1,3 @@ +public interface FibonacciNumberFactory { + public FibonacciNumber getFibonacciNumber(int num); +} diff --git a/entries/nritschel/fib-java/src/Main.java b/entries/nritschel/fib-java/src/Main.java new file mode 100644 index 0000000..8ae46f1 --- /dev/null +++ b/entries/nritschel/fib-java/src/Main.java @@ -0,0 +1,21 @@ +public class Main { + public static void main(String[] args) { + if (args.length < 1) { + System.out.println(""" + Please provide: + 1. fibonacci number to compute, and + 2. (optional) the calculation method (naive or cached)."""); + } + else { + FibonacciNumberFactory factory; + if (args.length >= 2 && args[1].equals("naive")) { + factory = new NaiveFibonacciNumberFactory(); + } + else { + factory = new CachedFibonacciNumberFactory(); + } + FibonacciCalculator calculator = new FibonacciCalculatorImpl(factory); + System.out.println(factory.getFibonacciNumber(Integer.parseInt(args[0])).calculate(calculator)); + } + } +} diff --git a/entries/nritschel/fib-java/src/NaiveFibonacciNumberFactory.java b/entries/nritschel/fib-java/src/NaiveFibonacciNumberFactory.java new file mode 100644 index 0000000..5185a8b --- /dev/null +++ b/entries/nritschel/fib-java/src/NaiveFibonacciNumberFactory.java @@ -0,0 +1,6 @@ +public class NaiveFibonacciNumberFactory implements FibonacciNumberFactory { + @Override + public FibonacciNumber getFibonacciNumber(int num) { + return new FibonacciNumber(num); + } +} |