How can I allow the end user to make a function in Java?
-
I am making a GUI in Java that needs to allow the end user to create (many) functions of a strictly mathematical variety. For now we can restrict ourselves to functions of one variable. So let's say that there's a pad of buttons with built-in functions and symbols such as + - * / ( ) ln() exp() et cetera, you get the idea. And the user creates a function, say f(x) = (x+1)/(x-1) Now ignoring difficulties like division by zero and things such as that, how would I store this function and then call it when the time comes? Should I create a Function class of my own? How would it work? Any ideas are valued. You don't need to post actual code; I can figure it out if you can give me an idea of where to go with this.
-
Answer:
I would create a Function object class, which stores the function in postfix (reverse polish) notation, in a queue. If you are not familiar with postfix notation, here's the Wikipedia article on it: http://en.wikipedia.org/wiki/Reverse_Polish_notation Postfix notation is nice because, once it's in postfix notation, it's easy to handle. You can just dequeue it and operate as you progress. For information on convert to postfix: http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm
Jason at Yahoo! Answers Visit the source
Other answers
A basic way to go about it would to allow the person to type in the function like you noted and store it in a variable in a container class. Then when the user wants to use that function, they can select it from a drop down list and your program can dynamically decide what values are needed for inputs. When it comes to actually computing the function, you may want to just parse the function string for keywords (or letters) like * + and variables like x and y and values like 7 and 42 and finally notations that control how the function is computed like parenthesis and brackets.
physicsdude2014
Related Q & A:
- How can I allow user to create posts in website using ASP.NET?Best solution by Programmers
- How can I tell if Windows user is still active?Best solution by answers.microsoft.com
- How can i continue my education in Canada in a international valid university?Best solution by Yahoo! Answers
- How can I use to prepaid cards to make a purchase?Best solution by Yahoo! Answers
- How can I allow only certain email addresses?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.