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!
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
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.
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).
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.
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.
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.
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
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!
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 ]
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.
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 -?