diff --git a/src/main/java/_4/_1/Function.java b/src/main/java/_4/_1/Function.java index 0483344..29c5ea4 100644 --- a/src/main/java/_4/_1/Function.java +++ b/src/main/java/_4/_1/Function.java @@ -2,22 +2,47 @@ package _4._1; public abstract class Function { - protected Function predecessor; + /** + * For functions f and g, + * g is the inner function of f in f(g(x)) + */ + protected Function inner; public Function() { - this.predecessor = null; + this.inner = null; } - public Function(Function predecessor) { - this.predecessor = predecessor; + public Function(Function inner) { + this.inner = inner; } + /** + * Note: This method should probably not be overridden. + *
+ * Computes the value of this (nested) function for x. + *
+ * If this function is f and it's inner function is g, + * then compute(x) returns f(g(x)) + * + * @see #apply(double) + */ public double calculate(double x) { - if(this.predecessor == null) { + if(this.inner == null) { return this.apply(x); } - return this.predecessor.calculate(x); + return this.inner.calculate(x); } + /** + * Computes the value of this function for x + *
+ * If this function is f and it's inner function is g, + * then apply(x) returns f(x) + */ + /* + * protected to allow the this method to manipulate state + * and still allow the developer to make assumptions about the state. + * In most cases the implementation can be public. + */ protected abstract double apply(double x); } diff --git a/src/main/java/_4/_1/SineFunction.java b/src/main/java/_4/_1/SineFunction.java index c2d6ace..cd49efb 100644 --- a/src/main/java/_4/_1/SineFunction.java +++ b/src/main/java/_4/_1/SineFunction.java @@ -5,8 +5,8 @@ public class SineFunction extends Function { super(); } - public SineFunction(Function predecessor) { - super(predecessor); + public SineFunction(Function inner) { + super(inner); } @Override diff --git a/src/main/java/_4/_1/SquareFunction.java b/src/main/java/_4/_1/SquareFunction.java index f07e58b..e27adfa 100644 --- a/src/main/java/_4/_1/SquareFunction.java +++ b/src/main/java/_4/_1/SquareFunction.java @@ -5,12 +5,12 @@ public class SquareFunction extends Function { super(); } - public SquareFunction(Function predecessor) { - super(predecessor); + public SquareFunction(Function inner) { + super(inner); } @Override - protected double apply(double x) { + public double apply(double x) { return x * x; } }