From 52db422b6c303b1b9018a85d842981ff427eece4 Mon Sep 17 00:00:00 2001 From: Umang Budhwar Date: Fri, 15 Jan 2021 04:34:00 +0530 Subject: [PATCH] BAEL-4695 - Evaluating a Math Expression in Java (#10345) * Added code for checking if a class is abstract or not. * Renamed test name as per review comments. * BAEL-4695 - Added code to evaluate math expressions. * BAEL-4695 - Added test case for math function. * BAEL-4695 Added consistent examples and entry in parent pom. * BAEL-4695 - Added more examples --- .../core-java-lang-math-3/pom.xml | 35 +++++++ .../EvalauteMathExpressionsUnitTest.java | 91 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 3 files changed, 127 insertions(+) create mode 100644 core-java-modules/core-java-lang-math-3/pom.xml create mode 100644 core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/evaluate/EvalauteMathExpressionsUnitTest.java diff --git a/core-java-modules/core-java-lang-math-3/pom.xml b/core-java-modules/core-java-lang-math-3/pom.xml new file mode 100644 index 0000000000..27c2372ab6 --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + core-java-lang-math-3 + 0.0.1-SNAPSHOT + core-java-lang-math-3 + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + + + + + net.objecthunter + exp4j + 0.4.8 + + + + com.fathzer + javaluator + 3.0.3 + + + + + + + + + diff --git a/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/evaluate/EvalauteMathExpressionsUnitTest.java b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/evaluate/EvalauteMathExpressionsUnitTest.java new file mode 100644 index 0000000000..a251e8d545 --- /dev/null +++ b/core-java-modules/core-java-lang-math-3/src/test/java/com/baeldung/math/evaluate/EvalauteMathExpressionsUnitTest.java @@ -0,0 +1,91 @@ +package com.baeldung.math.evaluate; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.fathzer.soft.javaluator.DoubleEvaluator; +import com.fathzer.soft.javaluator.StaticVariableSet; + +import net.objecthunter.exp4j.Expression; +import net.objecthunter.exp4j.ExpressionBuilder; + +public class EvalauteMathExpressionsUnitTest { + + @Test + public void givenSimpleExpression_whenCallEvaluateMethod_thenSuccess() { + Expression expression = new ExpressionBuilder("3+2").build(); + double result = expression.evaluate(); + Assertions.assertEquals(5, result); + } + + @Test + public void givenTwoVariables_whenCallEvaluateMethod_thenSuccess() { + Expression expression = new ExpressionBuilder("3x+2y").variables("x", "y") + .build() + .setVariable("x", 2) + .setVariable("y", 3); + double result = expression.evaluate(); + Assertions.assertEquals(12, result); + } + + @Test + public void givenMathFunctions_whenCallEvaluateMethod_thenSuccess() { + Expression expression = new ExpressionBuilder("sin(x)*sin(x)+cos(x)*cos(x)").variables("x") + .build() + .setVariable("x", 0.5); + double result = expression.evaluate(); + Assertions.assertEquals(1, result); + } + + @Test + public void givenExpression_whenCallEvaluateMethod_thenSuccess() { + String expression = "3+2"; + DoubleEvaluator eval = new DoubleEvaluator(); + Double result = eval.evaluate(expression); + Assertions.assertEquals(5, result); + } + + @Test + public void givenVariables_whenCallEvaluateMethod_thenSuccess() { + String expression = "3*x+2*y"; + DoubleEvaluator eval = new DoubleEvaluator(); + StaticVariableSet variables = new StaticVariableSet(); + variables.set("x", 2.0); + variables.set("y", 3.0); + Double result = eval.evaluate(expression, variables); + Assertions.assertEquals(12, result); + } + + @Test + public void givenMathFunction_whenCallEvaluateMethod_thenSuccess() { + String expression = "sin(x)*sin(x)+cos(x)*cos(x)"; + DoubleEvaluator eval = new DoubleEvaluator(); + StaticVariableSet variables = new StaticVariableSet(); + variables.set("x", 0.5); + Double result = eval.evaluate(expression, variables); + Assertions.assertEquals(1, result); + } + + @Test + public void givenJavaScriptingApiAndSimpleExpression_whenCallEvalMethod_thenSuccess() throws ScriptException { + ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); + ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript"); + String expression = "3+2"; + Integer result = (Integer) scriptEngine.eval(expression); + Assertions.assertEquals(5, result); + } + + @Test + public void givenJavaScriptingApi_whenCallEvalMethod_thenSuccess() throws ScriptException { + ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); + ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript"); + String expression = "x=2; y=3; 3*x+2*y;"; + Double result = (Double) scriptEngine.eval(expression); + Assertions.assertEquals(12, result); + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 140d5efe3b..2d342c4216 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -84,6 +84,7 @@ core-java-lang-3 core-java-lang-math core-java-lang-math-2 + core-java-lang-math-3 core-java-lang-oop-constructors core-java-lang-oop-patterns core-java-lang-oop-generics