jQuery in Action, 2nd edition
The moose likes Beginning Java and the fly likes Simple Calculator Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Simple Calculator" Watch "Simple Calculator" New topic
Author

Simple Calculator

Narendra kadimetla
Greenhorn

Joined: Mar 12, 2010
Posts: 1
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...


Naren Reddy.
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
What would the interface for the Add class be? How would you use an object of type Add?
Devaka Cooray
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2701
    
    3

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.


Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
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

Joined: Aug 25, 2008
Posts: 149

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: 32712
    
    4
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

Joined: Aug 25, 2008
Posts: 149

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: 32712
    
    4
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Simple Calculator
 
Similar Threads
Question on addTestSuite & addTest in JUnit
Trouble using addTestSuite() vs. addTest()
Switch Case Problem
toString() method
Need help with Overloading Methods