Results 1 to 8 of 8
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Graphing Function Ordered Pair Question

    I want the use to be able to enter a function such as f(x) = x^2 and then have a table with the values of points on that line but I am stuck.

    How would I convert the equation so I could plug variables into it.

    If I did this I would get a mismatch error.

    Java Code:
    String equation = "x^2";
    int x = 1;
    int solution = equation;
    Ideally, this would plug in the x variable and solve, but Java can't do that.
    If anyone has any ideas that would be great.

    Thank you for your help!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: Graphing Function Ordered Pair Question

    Unless you write a parser to analyze the expression you would have to code the method directly. Also, ^ is not exponentiation but bitwise exclusive or. There is no exponentiation operator in Java so you need to use Math.pow().

    But to give an example.

    Java Code:
    public double func_of_x(double x) {
        return 3 * x *x + 2 * x + 3;
    }
    Last edited by jim829; 05-29-2013 at 02:39 AM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Graphing Function Ordered Pair Question

    Is there a way to parse the equation though, so that way they could enter the equation and then I would be able to plug variables into it?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: Graphing Function Ordered Pair Question

    Yes. Parsing an equation is not overly difficult from a computer science perspective. One way would be to convert it to reverse Polish notation and then evaluate it using stack operations. Most second or third year students need to learn to do it when they write compilers. To do it well requires an understanding of finite state machines and lexical analysis. There may be some packages available to facilitate this but I do not know of any. Perhaps others in this forum could provide a better answer. But syntactically, I believe what you want is something like this.

    Java Code:
    double d;
    // assume ^ is exponentiation
    d = evaluate("x^2 + 5", 4);
    System.out.println("d = " + d);// should print 21.0
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Graphing Function Ordered Pair Question

    Thank you for your help. I'm only 14, so this may be a little difficult for me to grasp. If anyone else could help me with this parser that would be great. Again, thank you for you help.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: Graphing Function Ordered Pair Question

    You might try ANTLR which is a parser generator. I just found out about this specific one myself and it is written for Java. You might also want to look on the web for "parsing algebraic expressions." There may be some prepackaged solutions that you can install. As I said earlier it is not exceptionally hard but it is challenging, especially for someone just starting out.

    Regards.
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Graphing Function Ordered Pair Question

    OK. Thank you. I will check that out.

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

    Default Re: Graphing Function Ordered Pair Question

    I wrote a blog article once on expression parsing (and evaluation); click on the link near the top right of this reply; alternatively you can let Javascript do the dirty work. Since Java 1.6. the JRE comes bundled with a Javascript interpreter. Feed you function/expression to that interpreter and get the result back; wait, I must have a small demo somewhere ...

    Here it is:

    Java Code:
    import javax.script.Invocable;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
     
    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 (NoSuchMethodException nsme) {
            	nsme.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Do not allow duplicates in an ordered link list
    By wfsteadman in forum New To Java
    Replies: 1
    Last Post: 02-16-2013, 09:31 PM
  2. Ordered Vector Issue
    By mmonish91 in forum New To Java
    Replies: 8
    Last Post: 03-07-2012, 07:44 AM
  3. Random Function Question.
    By Dark in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 04:51 AM
  4. Ordered Array plz help....due tomorrow!!
    By canyonist in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 09:57 AM
  5. viewing records ordered by date
    By nitish.dhar in forum New To Java
    Replies: 3
    Last Post: 07-31-2008, 07:14 AM

Posting Permissions

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