# Reading a Math Equation in the console

• 06-10-2011, 04:08 AM
JonB319
Reading a Math Equation in the console
Hey all,
I have a quick question about reading in an equation. In one of my textbooks it has an exercise where it asks us to write a program that creates a table which displays the results of any given growth function. The table is just each part of the equation, and powers of ten results for each part, and then the last column having all the equation and the same numbers plugged in. Like this:
Number(n) 15n^2 45n 15n^2+45n
1 15 45 60
2 60 90 150
5 375 225 600
10 1500 450 1950
From there it just uses powers of ten.... Now my program has to take any given growth function and make a table like this out of it..... This is my second Java course and I have no idea how to do this. Any pointers anyone can throw me? The table is no big deal, but how can I read an equation from the console, and split up each part? It needs to able to take exponents, cubed, squared, and logarithm. I really appreciate any input I can get on this one, I have spent the last couple hours looking through books but I can't find anything. Thanks!
• 06-10-2011, 04:21 AM
littledrummerboy
Well i'm a little confused about the exact assignment but I think you should be able to use the Math class to do all those things, check this out: Math (Java 2 Platform SE v1.4.2)
for example Math.pow(2,3) takes 2 to the third power, and would return 8
it also has similar functions for logs, etc. so just scroll down and look through the methods
• 06-10-2011, 04:23 AM
littledrummerboy
if you could be more specific about the assignment or give me an example i may be able to provide more help but one possibility would be to read in the equation from the console as a string and then have your code break it up and convert it to integers or whatever you need
• 06-10-2011, 04:28 AM
Junky
What are you having trouble with: reading in the formula, performing calculations, formatting output, something else?
• 06-10-2011, 05:43 AM
DarrylBurke
Quote:

Originally Posted by littledrummerboy
Well i'm a little confused about the exact assignment but I think you should be able to use the Math class to do all those things, check this out: Math (Java 2 Platform SE v1.4.2)

Bookmark this one: Java Platform SE 6

Thanks, db
• 06-10-2011, 01:41 PM
JonB319
I do apologize, I meant to be more specific. What I don't know how to do is read the equation in from the console. If anyone could throw me some pointers I would really appreciate it!
• 06-10-2011, 01:53 PM
Norm
Quote:

how to do is read the equation in from the console
See the Scanner class for one way to read data from the console. Search this forum for many examples.
• 06-10-2011, 01:54 PM
JosAH
Quote:

Originally Posted by JonB319
I do apologize, I meant to be more specific. What I don't know how to do is read the equation in from the console. If anyone could throw me some pointers I would really appreciate it!

Since JAva 1.6 the JDK comes bundles with a Javascript engine; it can do the job for you; here's some code to play with:

Code:

```import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException;   public class ScriptDemo {       public static void main(String[] args) {                     ScriptEngineManager manager = new ScriptEngineManager();         ScriptEngine engine = manager.getEngineByName("JavaScript");                 try {             String expression = "function add(x, y) { return x+y; }";             engine.eval(expression);                             engine.put("a", 41);                             engine.put("b", 1);             Invocable invocable= (Invocable)engine;                         double d= (Double)engine.eval("add(a, b)");                         System.out.println(d);                         System.out.println("add(1, 41)= "+invocable.invokeFunction("add", 1, 41));                         System.out.println("add(a, b)= "+invocable.invokeFunction("add", "a", "b"));                         System.out.println("add(a, b)= "+engine.eval("add(a, b)"));         } catch(ScriptException se) {             se.printStackTrace();         } catch (NoSuchMethodException nsme) {                 nsme.printStackTrace();         }     } }```
kind regards,

Jos