• 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

Calculator - Stacks

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking at the calculator you can pull up on your desktop and that works with either input. Thanks.
 
Teri Fisher
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info! My assingment asks that Stacks be used in the calculator.
 
Rajkamal Pillai
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Teri Fisher
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yahoo it's working!!! Thanks so much for everyone's help!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done. Please post what you've got so others can learn from it.
 
Teri Fisher
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No comprende. My feelings are that this little project is not really worth anymore hassle. I am moving on to other projects.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Teri Fisher:
Not really worth anymore hassle.

Sounds a very sensible attitude.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic