I have been thinking of making a graphing calculator for practice. However, I do not know how to assign to a variable an expression. What I exactly mean is:

let's say x is a variable, and y a function of x. I can just define the function inside the code, but then, I'd have to change change the source code every time I want to change the function I want to plot. What I want to do is to be able to define y as a function of x through a GUI... I believe I should use JTextField class, but I was wondering if someone could give more accurate direction.

thanks in advance!

If your question is purely on GUI (how to take an expression as input):
- Make a list of functions supported by your code and build a GUI such that user can only use one of those.
- Fix the number of variables supported in an expression (1, 2, ...) and fix names for them (say X, Y, ...).
Let the user build the expressions using keywords from these 2 predefined lists.

If your question is how to represent/process/pass-around/store the expression in the code, then pre/post/in-fix tree are meant for this purpose and you cna use them. See some links here:
- http://en.wikipedia.org/wiki/Calculator_input_methods
- http://en.wikipedia.org/wiki/Reverse_Polish_notation
- http://en.wikipedia.org/wiki/Polish_notation
- http://en.wikipedia.org/wiki/Infix_notation

You can allow the user to type in an expression in JavaScript then use/call that as code dynamically at runtime using Java's recent support for scripting languages.

you can just make a function of every possible computation you want..

This sounds like a lisp-type problem. Have you considered clojure?

you can just make a function of every possible computation you want..

Hush, man, are you mad? Haven't you ever heard of the combinatorial explosion? Do you WANT him to incorporate all of the matter in the universe into one massive computer, just to satisfy his mad desires?

(There's a Charles Stross story here... or perhaps Iain M. Banks... or a collaboration...)

@Kiparsky,
I haven't considered clojure.

@James,
I'll have to look at your way of doing it.

Thank you all. I'll just mark this thread as solved. I think you helped me enough to get a clear idea on how I'll proceed. thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.