Results 1 to 8 of 8
  1. #1
    JonB319 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default 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!

  2. #2
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    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

  3. #3
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    What are you having trouble with: reading in the formula, performing calculations, formatting output, something else?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by littledrummerboy View Post
    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)
    Please don't post links to old, outdated Java API pages.

    Bookmark this one: Java Platform SE 6

    Thanks, db

  6. #6
    JonB319 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default 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!

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,879
    Rep Power
    25

    Default

    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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JonB319 View Post
    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:

    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();
            }
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with equation
    By _Jk_ in forum New To Java
    Replies: 10
    Last Post: 03-17-2011, 09:15 PM
  2. reading commands from console
    By merik in forum New To Java
    Replies: 9
    Last Post: 11-17-2010, 05:09 PM
  3. equation
    By bobo67 in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 07:40 PM
  4. Need help with math equation
    By annabellastorm in forum New To Java
    Replies: 4
    Last Post: 01-10-2010, 06:12 PM
  5. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 12:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •