Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Calculator

 
Narendra kadimetla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would the interface for the Add class be? How would you use an object of type Add?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4435
256
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
quite tricky though when using infix notation
you must have been thinking on the same lines.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic