Results 1 to 8 of 8
- 06-10-2011, 04:08 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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 #2
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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 #3
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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 #4
What are you having trouble with: reading in the formula, performing calculations, formatting output, something else?
- 06-10-2011, 05:43 AM #5
Please don't post links to old, outdated Java API pages.
Bookmark this one: Java Platform SE 6
Thanks, db
- 06-10-2011, 01:41 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Correction - My Bad
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 #7
See the Scanner class for one way to read data from the console. Search this forum for many examples.how to do is read the equation in from the console
- 06-10-2011, 01:54 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java 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(); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with equation
By _Jk_ in forum New To JavaReplies: 10Last Post: 03-17-2011, 08:15 PM -
reading commands from console
By merik in forum New To JavaReplies: 9Last Post: 11-17-2010, 04:09 PM -
equation
By bobo67 in forum New To JavaReplies: 5Last Post: 09-06-2010, 06:40 PM -
Need help with math equation
By annabellastorm in forum New To JavaReplies: 4Last Post: 01-10-2010, 05:12 PM -
Reading a line from console using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 11:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks