File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Real time objects to Java objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Real time objects to Java objects" Watch "Real time objects to Java objects" New topic
Author

Real time objects to Java objects

Ganesh Thanga
Greenhorn

Joined: May 14, 2008
Posts: 3
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

Joined: Apr 16, 2008
Posts: 2187
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

Joined: May 14, 2008
Posts: 3
But I have asked to create class structure with the inter-depencies for this problem. :-(
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 16, 2005
Posts: 14337
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

@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

Joined: Oct 27, 2005
Posts: 19755
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Real time objects to Java objects