Results 1 to 6 of 6
- 04-01-2011, 03:32 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
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
- 04-01-2011, 03:34 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
I don't see the point of this... why don't you just use parameters...
- 04-01-2011, 03:39 AM #3
You can write generic code to evaluate any expression. First you have to convert it from infix (3 + 2) to postfix (3 2 +).
- 04-01-2011, 04:51 AM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Just to give you pointers, you can follow these steps:
1. Define constants for each operator in your class, e.g.
2. write a method which creates a java file.Java Code:public static final String power = "Math.pow";
3. Now start evaluating the expression entered by user, which includes:Java Code:File generatedfile = new File("Generator.java"); FileWriter writer= new FileWriter(generatedfile );
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
- 04-01-2011, 08:10 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,404
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-01-2011, 12:05 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Method Writing Help
By socboy6579 in forum New To JavaReplies: 5Last Post: 11-16-2010, 09:14 PM -
Writing a method
By Proton in forum New To JavaReplies: 10Last Post: 05-13-2010, 02:03 PM -
I need some help in writing a method
By loraineEd in forum New To JavaReplies: 2Last Post: 01-02-2010, 04:23 PM -
Writing a recursive method :S
By Thousand in forum New To JavaReplies: 1Last Post: 12-06-2009, 03:13 PM -
Writing the filter method
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-03-2007, 03:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks