Tuesday, March 16, 2010

Expression Handling

I intended to post the whole Java source code but unfortunately, there are 8 files in total so I think it will be better if you get them from MediaFire. Don't worry, there are only ~500 lines of code and I wrote comment for every single method.

Actually, the main class (Expression) design isn't done by me 100% because I have to follow the assignment requirement (also in the uploaded file). I don't really understand why we have to have 2 constructors and then another append() method. Really, I can't get it. Tell me if you have an idea.

Anyway, using the Expression class is very simple. Here is how I tested it:

Please note that it will run but an Exception will be thrown about variable b hasn't been initialized yet. You can either remove b from the append() method or use setValue() method to assign an value for the variable.

Also note that: currently the class support adding, subtracting, multiplying and dividing. Functions can be supported easily by writing a new class (ExpressionFunction?) and change the RPNize() static method (using Shunting-yard algorithm) a little bit. All comments are welcome. Thank you for reading