Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Real time objects to Java objects

 
Ganesh Thanga
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is not the best computer language for executing mathematical equations. There are some math functions bulit in at http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have asked to create class structure with the inter-depencies for this problem. :-(
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good start to learning how to build a parser using Java-based objects would be to read Building Parsers with Java written be John Metsker.

http://www.amazon.com/Building-Parsers-With-Steven-Metsker/dp/0201719622
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's simple equations like these, I would have told them I'd simply use a JavaScript ScriptEngine and let that do the hard work for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic