permaculture playing cards*
The moose likes Beginning Java and the fly likes Calculator - Stacks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculator - Stacks" Watch "Calculator - Stacks" New topic
Author

Calculator - Stacks

Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I am trying to create a calculator and need to use two stacks, I have a stack for numbers and one for operators but I'm wondering if I need another stack to pop everything into??? Anyone have any suggestions for me and am I going about this wrong? I have also added the Stack methods pop, peek, push and empty.
Thanks in advance!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Who told you to get the surrounding class to implement ActionListener? Go and see some of my recent posts on Swing; your actionPerformed method shows just what can happen. Probably best to create a NumberActionListener class which implements ActionListener, then you can have

btn1.addActionListener(new NumberActionListener(1));
btn2.addActionListener(new NumberActionListener(2));

etc.

Are you only using single-digit numbers?
Why not append the number to the text in the TextArea? I can't remember, but there are some text Components where you can append to what is already written. You will have to check which, yourself.

Don't use null Layout if you can. You can probably get all your buttons onto a GridLayout.

Don't use new Font("Dialog", Font.BOLD, 18) repeatedly. Use
Font myFont = new Font("Dialog", Font.BOLD, 18); and
btnX.setFont(myFont);

You don't want push peek or pop methods in your Applet (and surely you would extend JApplet?). You need a Stack (you can blind your teacher with science and use ArrayDeque instead of java.util.Stack: I kid you not). You have to push numbers onto it, and pop them when you have an operator.

If you are not worrying about proper algebraic logic, you may be able to get away with your single stack.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
BTW: You can't push 9.0 onto a Stack; you can only put Objects onto a Stack. If you parametrise it as Stack<Double> you can use autoboxing to push(1.0).
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I first started out with a GridLayout, then I went to BorderLayout and then I went to this layout becuause before this I couldn't get the layout the way I wanted it to look. The project calls for a text field to enter a double number, perhaps it just meant 10 instead of 10.0, when I read double I think Double. Thanks for your advise, I will look at your Swing posts and start over again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
If you are using buttons, why are you putting the number into the text field? Please read the assignment specification very carefully to see whether that is what it means.
I still think GridLayout on a JPanel would work better. Of course if you knew GridBag . . .
Look on Cai Horstmann's website for the helper class to make GridBag easier.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I was looking at the calculator you can pull up on your desktop and that works with either input. Thanks.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
Well, I guess I don't have time to start over, I need to just make this one work and hand it in if that is possible. If I can just make a simple 2+4 calculation work it will be acceptable but I am still confused as how to pop them out of the stack and my current changes are not working.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi Teri,

I suggest you familiarize yourself with data structures (tree). The algorithm used for expression evaluation in computer languages is based on creation of a tree structure from the expression and then traversing it. Check out this article I think it's helpful => http://www.arstdesign.com/articles/expression_evaluation.html

Cheers,
Raj.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
Thanks for the info! My assingment asks that Stacks be used in the calculator.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Check out these sites (expression evaluation using stacks):
http://en.wikipedia.org/wiki/Stack_(data_structure)
http://www.codeproject.com/KB/cpp/rpnexpressionevaluator.aspx
http://cis.stvincent.edu/html/tutorials/swd/stacks/stacks.html
http://www.cis.upenn.edu/~matuszek/cit594-2002/Assignments/5-expressions.html

they have pseudo code I think for the desired logic you need.

Cheers,
Raj.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
I have made some adjustments, it will compile but when the equals sign is pressed in the applet it doesn't work. Am I not calling my evaluateExpression() correctly? Thanks for any assistance!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41629
    
  55
The code allocates a new Stack object each time the action listener runs - not what you want. "nums" should be an instance field instead.

You're also displaying the string "nums" instead of the result returned by the evaluateExpression method.
[ May 13, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
Yahoo it's working!!! Thanks so much for everyone's help!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Well done. Please post what you've got so others can learn from it.
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
This calculator only allows entry of one number, the operator and another number. It also is lacking more error handling among other things. I learned that there is a much better way to go about this but......it works.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Yes, it does work.

Is there any way you can set up a number, then when you click on button1 your number is multiplied by 10 and 1 added? Then push the number when you click on + or -?
Teri Fisher
Ranch Hand

Joined: Jan 07, 2008
Posts: 52
No comprende. My feelings are that this little project is not really worth anymore hassle. I am moving on to other projects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Originally posted by Teri Fisher:
Not really worth anymore hassle.
Sounds a very sensible attitude.
 
jQuery in Action, 2nd edition
 
subject: Calculator - Stacks