This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Problem with calculator

Lovro Posaric
Greenhorn
Posts: 11
Hey all!
This is my first post on this forum
I've got problem with making my calculator...
Here is the code:
GUI:

Engine:

I've made some "stupid" fixes today(I'mean: I hadn't idea what should I do,so I tried this)...

Campbell Ritchie
Sheriff
Posts: 50687
83
Welcome to the Ranch

That looks like code from somebody who has dived in and written code without getting any design. You need to work out how a calculator will work, and start by implementing the part which does the arithmetic. You will find it a lot easier if you confine yourself to 1 + 2 and forget about complicated arithmetic like 1 + 2 × 3. Work out how you will do arithmetic like 123 + 456. How are you going to make the numbers up from individual digits? How are you going to return the result? Don’t try a GUI until you have got that all working.

I would suggest you delete the calculator listener completely and start again. What you want is new object‑oriented code. You want listeners for operators and listeners for operands.
What you are doing is adding a listener which takes the 2 from the button and uses that to set up its own values. It would be a lot easier if you had the 10 number buttons in an array, then you can useBut I think you will take all of today getting the logic for the arithmetic working, so that is something for tomorrow!

Lovro Posaric
Greenhorn
Posts: 11
Thanks!

I'm bit confused now...but I will try to do something...

Campbell Ritchie
Sheriff
Posts: 50687
83
That is how I suggest you try your Calculator, without a GUI. That should give the result of 579. True algebraic logic requires much more thought. To get 1 2 × 3 to produce the correct result of 7 is much more complicated, which would involve pushing 1, +, and 2 onto a stack before the ×, and is much more complicated.
You will obviously have to create Calculator and Operators classes (well, Operators is probably an enum), or similar. Get that working, and do not consider a GUI until that program gives correct results.

Lovro Posaric
Greenhorn
Posts: 11
I got idea for using NetBeans and finnaly finished calculator