Forums Register Login

Calculator - Stacks

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I was looking at the calculator you can pull up on your desktop and that works with either input. Thanks.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for the info! My assingment asks that Stacks be used in the calculator.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Yahoo it's working!!! Thanks so much for everyone's help!
+Pie Number of slices to send: Send
Well done. Please post what you've got so others can learn from it.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 -?
+Pie Number of slices to send: Send
No comprende. My feelings are that this little project is not really worth anymore hassle. I am moving on to other projects.
+Pie Number of slices to send: Send
 

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

Sounds a very sensible attitude.
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2124 times.
Similar Threads
problem with program
calculator applet
another question on calculator applet
help
what each part of this program does
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:36:48.