Results 1 to 6 of 6
  1. #1
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Arrow Writing a method to write a method?

    I am curious if there is a way to use a string that's syntactically the same as the body of a method to define a method by the program. I wanted to make a program where the user is prompted to type in a function (3x^2+(2x-3)^.5 for example) and then have the program create a class or method to call in order to evaluate the function at different x-values. this is easy if one knows the function ahead of time--just write it inside a method in proper syntax and have the method return the answer--but I don't know how or if I can (after "translating" the above into something like 3*Math.pow(x,2)+Math.pow(2*x-3,.5)) code the program to create a new method or a class with a method that returns the function evaluated at a given x. Any pointers or directions to go with this would be greatly appreciated :D

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I don't see the point of this... why don't you just use parameters...

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

    Default

    You can write generic code to evaluate any expression. First you have to convert it from infix (3 + 2) to postfix (3 2 +).

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Just to give you pointers, you can follow these steps:
    1. Define constants for each operator in your class, e.g.
    Java Code:
    public static final String power = "Math.pow";
    2. write a method which creates a java file.
    Java Code:
    File generatedfile = new File("Generator.java");
          FileWriter writer= new FileWriter(generatedfile );
    3. Now start evaluating the expression entered by user, which includes:
    a. find the operator entered by user,
    b. find the corresponding String defined by you, i.e if user entes ^, the you know it means Math.pow is to be used. You will have to use if else in your code for all the possible operators.
    c. Now write the generated string to java file created above.
    4. Make sure you write all the writing all the modifiers properly in the generated file.
    5. Close the filewriter object.

    You are done

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

    Default

    You have a couple of options:

    1) write your own interpreter for the language you want to implement; advantage: you can do what you want; disadvantage: you have to implement it all yourself.

    2) use the built-in Javascript interpreter; advantage: it has all been written for you; disadvantage: you're bound to Javascript.

    3) use beanshell it's a Java interpreter; advantage: it's Java but interpreted; disadvantage: 'vendor lock in'.

    I'd personally go for option 3). Beanshell integrates perfectly with your own Java code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    balla is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks all, This helps a lot :D what class do I use to execute the code generated in lovelash's example?

Similar Threads

  1. Method Writing Help
    By socboy6579 in forum New To Java
    Replies: 5
    Last Post: 11-16-2010, 09:14 PM
  2. Writing a method
    By Proton in forum New To Java
    Replies: 10
    Last Post: 05-13-2010, 02:03 PM
  3. I need some help in writing a method
    By loraineEd in forum New To Java
    Replies: 2
    Last Post: 01-02-2010, 04:23 PM
  4. Writing a recursive method :S
    By Thousand in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 03:13 PM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 03:32 PM

Tags for this Thread

Posting Permissions

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