Hi,
I have less experience in java designing. In one of the interviews I have been asked a question which is like ,Design/provide a java solutions for the expression '(a+b)*(a-b)-c' ?
I went explaining that we could create classes like Operators,Operands and Expression and saying that the class Expression would be having Operators and Operands and child expression.
Any suggestion how to visualize the real time problem in terms of java.For example for the above question whether to make the class abstract/or have an interface and so on?
The question is not clear enough, and as written does not mention anything about creating Classes. A basic approach would be to (1) create variables a, b, c, (2) populate them with values, and (3) execute the equation. Very simple.
Ganesh Thanga
Greenhorn
Joined: May 14, 2008
Posts: 3
posted
0
But I have asked to create class structure with the inter-depencies for this problem. :-(
One normally writes a compiler to parse expressions. And then some code to evaluate/execute the compiled code.
In Java, you always have access to the Java compiler, so if the expressions were to be evaluated as if they were Java, you could use the compiler, and then execute the resulting class files.
Writing a compiler is fairly advanced Computer Science. I would expect a very good student to be able to do this if they graduated from a good University undergraduate program, and I would expect that all students who have a Masters of CS to be able to do it. OR at least talk about how they would do it. Actually doing it probably would take a few months.
Using the Java compiler within a Java program is an advanced technique that I would not expect most professional Java programmers to know about. But a Senior person, it would be good if they could talk about it.
Note, this has nothing to do with most standard definitions of "real time" computing
Ganesh Thanga wrote:I went explaining that we could create classes like Operators,Operands and Expression and saying that the class Expression would be having Operators and Operands and child expression.
If you want to create a program that can parse and evaluate expressions, that sounds like a good start. Think about what kinds of things there can be in an expression: operators, numbers, variables, subexpressions etc. Now think about how these things fit together. What exactly does an expression consist of? How do operators connect parts of an expression?
To give you an idea of what you could do, look for the description of "expression trees" on this page. Once you've parsed the expression into an expression tree, you can let your program do useful things with it. Here is another example.
@Ganesh: I think you mean "real-world", not "real-time". The term "real-time" has a specific standard meaning in Computer Science, and it's not what you're talking about.