diff --git a/src/main/java/formula/Constant.java b/src/main/java/formula/Constant.java new file mode 100644 index 0000000000000000000000000000000000000000..12359a7ee721948a677a54a091215e65d5c3b5bc --- /dev/null +++ b/src/main/java/formula/Constant.java @@ -0,0 +1,62 @@ +package formula; + +import java.util.HashMap; + +public class Constant implements Formula { + + private double value; + + public Constant(double value) { + // TODO : change the code. + } + + /** + * Compute the value of the formula + * + * @param xValue the value of the variable x + * @return the value of the function when the variable x has value {@code xValue} + */ + @Override + public double eval(double xValue) { + // TODO : change the code. + return 0; + } + + /** + * Compute the derivative of the formula. + * + * @return the derivative of the formula + */ + @Override + public Formula derivative() { + // TODO : change the code. + return this; + } + + /** + * Return a {@code String} representation of the formula. + * + * @return the formula as a {@code String} + */ + @Override + public String toString() { + // TODO : change the code. + return "toto"; + } + + /** + * Indicates whether some other object is "equal to" this one. + * + * @param obj the reference object with which to compare. + * @return {@code true} if this object is the same as the obj + * argument; {@code false} otherwise. + * @see #hashCode() + * @see HashMap + */ + @Override + public boolean equals(Object obj) { + if(obj == null) return false; + if(!(obj instanceof Constant constant)) return false; + return this.value == constant.value; + } +} diff --git a/src/main/java/viewer/FunctionList.java b/src/main/java/viewer/FunctionList.java index 85ccac46af98bd93e3c756f30eb48f5964c7e938..68d0c3485c957d0b831ccbe92e99897892706741 100644 --- a/src/main/java/viewer/FunctionList.java +++ b/src/main/java/viewer/FunctionList.java @@ -1,5 +1,6 @@ package viewer; +import formula.Constant; import javafx.scene.chart.XYChart; import java.util.ArrayList; @@ -18,9 +19,8 @@ class FunctionList { this.lowerBound = functionChart.getLowerBound(); this.upperBound = functionChart.getUpperBound(); - // TODO: add functions - - + PlottableFunction function = new PlottableFunction(new Constant(1), "f"); + addFunctionAndItsDerivative(function); } void toggleFunction(PlottableFunction function) { diff --git a/src/main/java/viewer/MainAppController.java b/src/main/java/viewer/MainAppController.java index fa240197e00ec9a2e97d1aa9f8052e886d1056c6..329cd94050ed206717deeca17a3b11859e8559d1 100644 --- a/src/main/java/viewer/MainAppController.java +++ b/src/main/java/viewer/MainAppController.java @@ -45,6 +45,7 @@ public class MainAppController implements Initializable { Button button = new Button(function.toString()); addButton(button); button.setOnAction(event -> toggleFunction(function)); + toggleFunction(function); } private void toggleFunction(PlottableFunction function){ diff --git a/src/test/java/ConstantTest.java b/src/test/java/ConstantTest.java new file mode 100644 index 0000000000000000000000000000000000000000..71eed1b8bbcc182dc9c5a376920134fa705c2b9d --- /dev/null +++ b/src/test/java/ConstantTest.java @@ -0,0 +1,42 @@ +import formula.Constant; +import formula.Formula; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; + +public class ConstantTest { + @Test + public void testEval(){ + Constant constantTen = new Constant(10); + assertThat(constantTen.eval(0.)).isCloseTo(10., within(.001)); + Constant constantOne = new Constant(1); + assertThat(constantOne.eval(10.)).isCloseTo(1., within(.001)); + } + + @Test + public void testToString(){ + Constant constantTen = new Constant(10); + assertThat(constantTen.toString()).isEqualTo("10.0"); + Constant constantOne = new Constant(1); + assertThat(constantOne.toString()).isEqualTo("1.0"); + } + + @Test + public void testEquals() { + Constant constantTen = new Constant(10); + Constant constantTen2 = new Constant(10); + Constant constantOne = new Constant(0); + assertThat(constantTen).isEqualTo(constantTen); + assertThat(constantTen).isEqualTo(constantTen2); + assertThat(constantTen).isNotEqualTo(constantOne); + } + + @Test + public void testDerivative() { + Constant constantTen = new Constant(10); + Formula derivedConstant = constantTen.derivative(); + assertThat(derivedConstant.eval(10.)).isCloseTo(0., within(.001)); + Constant constantZero = new Constant(0); + assertThat(derivedConstant).isEqualTo(constantZero); + } +}