I am new to JAVA.I was asked to build a simple Add class and i did it this way..
He then asked me to do Subtraction,Multiplication and Divison.I did them.
Now he wants me build a simple calculator using all these classes.
I am not getting anything...please help me...
This message was edited 2 times. Last update was at by Bear Bibeault
Naren Reddy.
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
posted
0
What would the interface for the Add class be? How would you use an object of type Add?
Welcome to JavaRanch Please UseCodeTags when you post a code. It's unnecessarily hard to read the code otherwise.
Please edit your post to add code tags by clicking the button.
Campbell Ritchie wrote:You need to pass values to the add method, and that returns a result.
Are you sure you should have an Add class, rather than an Arithmetic class with a (static) add method?
This is definitely the way to go for all of your supported calculator operations.
Reorganizing the stack from the user input can be quite tricky though when using
infix notation. It might be a lot easier to enhance functionality step by step using
Reverse Polish Notation (RPN) internally.
You might wanna have a look here.
Good luck and please keep us posted
Matt
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
Is reverse Polish notation the same as postfix notation? Agree it is better, but it requires parsing skills, so it may be difficult to implement.
Campbell Ritchie wrote:Is reverse Polish notation the same as postfix notation? Agree it is better, but it requires parsing skills, so it may be difficult to implement.
that's affirmative.
Postfix notation is just another name for RPN.
Regarding the parsing:
from my point of view it is much more difficult to
parse the entire infix statement than just stack
RPN instructions.
Just an opinion.
Matt
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
Agree it is much easier to put postfix/reverse Polish onto a stack, which I am doing myself in FORTH, but it does mean learning notation like 1 2 3 * + instead of 1 + 2 * 3. When I said "parsing skills" I thought Narendra kadimetla wanted to start with an infix expression (eg 1 + 2 * 3) and evaluate that correctly. When you said