1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Rename Number to NumberInterface. Fix factorial function. Add toString override to NaiveNumber.

This commit is contained in:
Arthur Drobot 2017-07-25 13:58:09 -07:00
parent 55c7f939d8
commit 4962aae083
5 changed files with 53 additions and 50 deletions

10
src/org/nwapw/abacus/number/Function.java Normal file → Executable file
View File

@ -4,15 +4,15 @@ import java.util.HashMap;
public abstract class Function { public abstract class Function {
private static final HashMap<Class<? extends Number>, Integer> priorityMap = private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
new HashMap<Class<? extends Number>, Integer>() {{ new HashMap<Class<? extends NumberInterface>, Integer>() {{
put(NaiveNumber.class, 0); put(NaiveNumber.class, 0);
}}; }};
protected abstract boolean matchesParams(Number[] params); protected abstract boolean matchesParams(NumberInterface[] params);
protected abstract Number applyInternal(Number[] params); protected abstract NumberInterface applyInternal(NumberInterface[] params);
public Number apply(Number...params) { public NumberInterface apply(NumberInterface...params) {
if(!matchesParams(params)) return null; if(!matchesParams(params)) return null;
return applyInternal(params); return applyInternal(params);
} }

View File

@ -9,13 +9,13 @@ public class FunctionDatabase {
private void registerDefault(){ private void registerDefault(){
functions.put("+", new Function() { functions.put("+", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1; return params.length >= 1;
} }
@Override @Override
protected Number applyInternal(Number[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
Number sum = params[0]; NumberInterface sum = params[0];
for(int i = 1; i < params.length; i++){ for(int i = 1; i < params.length; i++){
sum = sum.add(params[i]); sum = sum.add(params[i]);
} }
@ -25,25 +25,25 @@ public class FunctionDatabase {
functions.put("-", new Function() { functions.put("-", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2; return params.length == 2;
} }
@Override @Override
protected Number applyInternal(Number[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].subtract(params[1]); return params[0].subtract(params[1]);
} }
}); });
functions.put("*", new Function() { functions.put("*", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1; return params.length >= 1;
} }
@Override @Override
protected Number applyInternal(Number[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
Number product = params[1]; NumberInterface product = params[1];
for(int i = 1; i < params.length; i++){ for(int i = 1; i < params.length; i++){
product = product.multiply(params[i]); product = product.multiply(params[i]);
} }
@ -53,29 +53,28 @@ public class FunctionDatabase {
functions.put("/", new Function() { functions.put("/", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2; return params.length == 2;
} }
@Override @Override
protected Number applyInternal(Number[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].divide(params[1]); return params[0].divide(params[1]);
} }
}); });
functions.put("!", new Function() { functions.put("!", new Function() {
@Override @Override
protected boolean matchesParams(Number[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 1; return params.length == 1;
} }
@Override @Override
protected Number applyInternal(Number[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
Number factorial = params[0]; NumberInterface factorial = params[0];
Number multiplier = params[0]; NumberInterface multiplier = params[0];
//It is necessary to later prevent calls of factorial on anything but non-negative integers. //It is necessary to later prevent calls of factorial on anything but non-negative integers.
while(multiplier.signum() == 1){ while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()));
factorial = factorial.multiply(multiplier); factorial = factorial.multiply(multiplier);
} }
return factorial; return factorial;

View File

@ -1,6 +1,6 @@
package org.nwapw.abacus.number; package org.nwapw.abacus.number;
public class NaiveNumber implements Number { public class NaiveNumber implements NumberInterface {
private double value; private double value;
@ -8,8 +8,8 @@ public class NaiveNumber implements Number {
this.value = value; this.value = value;
} }
static final NaiveNumber ZERO = new NaiveNumber(0); public static final NaiveNumber ZERO = new NaiveNumber(0);
static final NaiveNumber ONE = new NaiveNumber(1); public static final NaiveNumber ONE = new NaiveNumber(1);
@Override @Override
public int precision() { public int precision() {
@ -17,33 +17,33 @@ public class NaiveNumber implements Number {
} }
@Override @Override
public Number multiply(Number multiplier) { public NumberInterface multiply(NumberInterface multiplier) {
return new NaiveNumber(value * ((NaiveNumber)multiplier).value); return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
} }
@Override @Override
public Number divide(Number divisor) { public NumberInterface divide(NumberInterface divisor) {
return new NaiveNumber(value / ((NaiveNumber)divisor).value); return new NaiveNumber(value / ((NaiveNumber)divisor).value);
} }
@Override @Override
public Number add(Number summand) { public NumberInterface add(NumberInterface summand) {
return new NaiveNumber(value + ((NaiveNumber)summand).value); return new NaiveNumber(value + ((NaiveNumber)summand).value);
} }
@Override @Override
public Number subtract(Number subtrahend) { public NumberInterface subtract(NumberInterface subtrahend) {
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value); return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
} }
@Override @Override
public Number negate() { public NumberInterface negate() {
return new NaiveNumber(-value); return new NaiveNumber(-value);
} }
@Override @Override
public Number intPow(int exponent) { public NumberInterface intPow(int exponent) {
Number power = this; NumberInterface power = this;
for(int currentExponent = 1; currentExponent <= exponent; currentExponent++){ for(int currentExponent = 1; currentExponent <= exponent; currentExponent++){
power = power.multiply(this); power = power.multiply(this);
} }
@ -51,7 +51,7 @@ public class NaiveNumber implements Number {
} }
@Override @Override
public int compareTo(Number number) { public int compareTo(NumberInterface number) {
NaiveNumber num = (NaiveNumber) number; NaiveNumber num = (NaiveNumber) number;
return Double.compare(value, num.value); return Double.compare(value, num.value);
} }
@ -62,10 +62,14 @@ public class NaiveNumber implements Number {
} }
@Override @Override
public Number promoteTo(Class<? extends Number> toClass) { public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
if(toClass == this.getClass()) return this; if(toClass == this.getClass()) return this;
return null; return null;
} }
public String toString(){
return Double.toString(value);
}
} }

View File

@ -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<? extends Number> toClass);
}

View File

@ -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<? extends NumberInterface> toClass);
}