From 4962aae083d0afd73dd451893074f192c3269876 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Tue, 25 Jul 2017 13:58:09 -0700 Subject: [PATCH] Rename Number to NumberInterface. Fix factorial function. Add toString override to NaiveNumber. --- src/org/nwapw/abacus/number/Function.java | 10 +++--- .../nwapw/abacus/number/FunctionDatabase.java | 31 +++++++++---------- src/org/nwapw/abacus/number/NaiveNumber.java | 28 ++++++++++------- src/org/nwapw/abacus/number/Number.java | 17 ---------- .../nwapw/abacus/number/NumberInterface.java | 17 ++++++++++ 5 files changed, 53 insertions(+), 50 deletions(-) mode change 100644 => 100755 src/org/nwapw/abacus/number/Function.java delete mode 100755 src/org/nwapw/abacus/number/Number.java create mode 100755 src/org/nwapw/abacus/number/NumberInterface.java diff --git a/src/org/nwapw/abacus/number/Function.java b/src/org/nwapw/abacus/number/Function.java old mode 100644 new mode 100755 index 807bea7..63f1ad4 --- a/src/org/nwapw/abacus/number/Function.java +++ b/src/org/nwapw/abacus/number/Function.java @@ -4,15 +4,15 @@ import java.util.HashMap; public abstract class Function { - private static final HashMap, Integer> priorityMap = - new HashMap, Integer>() {{ + private static final HashMap, Integer> priorityMap = + new HashMap, Integer>() {{ put(NaiveNumber.class, 0); }}; - protected abstract boolean matchesParams(Number[] params); - protected abstract Number applyInternal(Number[] params); + protected abstract boolean matchesParams(NumberInterface[] params); + protected abstract NumberInterface applyInternal(NumberInterface[] params); - public Number apply(Number...params) { + public NumberInterface apply(NumberInterface...params) { if(!matchesParams(params)) return null; return applyInternal(params); } diff --git a/src/org/nwapw/abacus/number/FunctionDatabase.java b/src/org/nwapw/abacus/number/FunctionDatabase.java index 909c9b7..acab4f1 100755 --- a/src/org/nwapw/abacus/number/FunctionDatabase.java +++ b/src/org/nwapw/abacus/number/FunctionDatabase.java @@ -9,13 +9,13 @@ public class FunctionDatabase { private void registerDefault(){ functions.put("+", new Function() { @Override - protected boolean matchesParams(Number[] params) { + protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; } @Override - protected Number applyInternal(Number[] params) { - Number sum = params[0]; + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface sum = params[0]; for(int i = 1; i < params.length; i++){ sum = sum.add(params[i]); } @@ -25,25 +25,25 @@ public class FunctionDatabase { functions.put("-", new Function() { @Override - protected boolean matchesParams(Number[] params) { + protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; } @Override - protected Number applyInternal(Number[] params) { + protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].subtract(params[1]); } }); functions.put("*", new Function() { @Override - protected boolean matchesParams(Number[] params) { + protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; } @Override - protected Number applyInternal(Number[] params) { - Number product = params[1]; + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface product = params[1]; for(int i = 1; i < params.length; i++){ product = product.multiply(params[i]); } @@ -53,29 +53,28 @@ public class FunctionDatabase { functions.put("/", new Function() { @Override - protected boolean matchesParams(Number[] params) { + protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; } @Override - protected Number applyInternal(Number[] params) { + protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].divide(params[1]); } }); functions.put("!", new Function() { @Override - protected boolean matchesParams(Number[] params) { + protected boolean matchesParams(NumberInterface[] params) { return params.length == 1; } @Override - protected Number applyInternal(Number[] params) { - Number factorial = params[0]; - Number multiplier = params[0]; + protected NumberInterface applyInternal(NumberInterface[] params) { + NumberInterface factorial = params[0]; + NumberInterface multiplier = params[0]; //It is necessary to later prevent calls of factorial on anything but non-negative integers. - while(multiplier.signum() == 1){ - multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass())); + while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){ factorial = factorial.multiply(multiplier); } return factorial; diff --git a/src/org/nwapw/abacus/number/NaiveNumber.java b/src/org/nwapw/abacus/number/NaiveNumber.java index 9dcc90b..ec9e6ba 100755 --- a/src/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/org/nwapw/abacus/number/NaiveNumber.java @@ -1,6 +1,6 @@ package org.nwapw.abacus.number; -public class NaiveNumber implements Number { +public class NaiveNumber implements NumberInterface { private double value; @@ -8,8 +8,8 @@ public class NaiveNumber implements Number { this.value = value; } - static final NaiveNumber ZERO = new NaiveNumber(0); - static final NaiveNumber ONE = new NaiveNumber(1); + public static final NaiveNumber ZERO = new NaiveNumber(0); + public static final NaiveNumber ONE = new NaiveNumber(1); @Override public int precision() { @@ -17,33 +17,33 @@ public class NaiveNumber implements Number { } @Override - public Number multiply(Number multiplier) { + public NumberInterface multiply(NumberInterface multiplier) { return new NaiveNumber(value * ((NaiveNumber)multiplier).value); } @Override - public Number divide(Number divisor) { + public NumberInterface divide(NumberInterface divisor) { return new NaiveNumber(value / ((NaiveNumber)divisor).value); } @Override - public Number add(Number summand) { + public NumberInterface add(NumberInterface summand) { return new NaiveNumber(value + ((NaiveNumber)summand).value); } @Override - public Number subtract(Number subtrahend) { + public NumberInterface subtract(NumberInterface subtrahend) { return new NaiveNumber(value - ((NaiveNumber)subtrahend).value); } @Override - public Number negate() { + public NumberInterface negate() { return new NaiveNumber(-value); } @Override - public Number intPow(int exponent) { - Number power = this; + public NumberInterface intPow(int exponent) { + NumberInterface power = this; for(int currentExponent = 1; currentExponent <= exponent; currentExponent++){ power = power.multiply(this); } @@ -51,7 +51,7 @@ public class NaiveNumber implements Number { } @Override - public int compareTo(Number number) { + public int compareTo(NumberInterface number) { NaiveNumber num = (NaiveNumber) number; return Double.compare(value, num.value); } @@ -62,10 +62,14 @@ public class NaiveNumber implements Number { } @Override - public Number promoteTo(Class toClass) { + public NumberInterface promoteTo(Class toClass) { if(toClass == this.getClass()) return this; return null; } + public String toString(){ + return Double.toString(value); + } + } diff --git a/src/org/nwapw/abacus/number/Number.java b/src/org/nwapw/abacus/number/Number.java deleted file mode 100755 index f0d1386..0000000 --- a/src/org/nwapw/abacus/number/Number.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.nwapw.abacus.number; - -public interface Number { - - int precision(); - Number multiply(Number multiplier); - Number divide(Number divisor); - Number add(Number summand); - Number subtract(Number subtrahend); - Number negate(); - Number intPow(int exponent); - int compareTo(Number number); - int signum(); - - Number promoteTo(Class toClass); - -} diff --git a/src/org/nwapw/abacus/number/NumberInterface.java b/src/org/nwapw/abacus/number/NumberInterface.java new file mode 100755 index 0000000..e9cf59b --- /dev/null +++ b/src/org/nwapw/abacus/number/NumberInterface.java @@ -0,0 +1,17 @@ +package org.nwapw.abacus.number; + +public interface NumberInterface { + + int precision(); + NumberInterface multiply(NumberInterface multiplier); + NumberInterface divide(NumberInterface divisor); + NumberInterface add(NumberInterface summand); + NumberInterface subtract(NumberInterface subtrahend); + NumberInterface negate(); + NumberInterface intPow(int exponent); + int compareTo(NumberInterface number); + int signum(); + + NumberInterface promoteTo(Class toClass); + +}