• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Calculator

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would the interface for the Add class be? How would you use an object of type Add?
 
Sheriff
Posts: 7135
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic