aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with calculator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with calculator" Watch "Problem with calculator" New topic
Author

Problem with calculator

Lovro Posaric
Greenhorn

Joined: Jul 01, 2012
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)...


Please help me!:$














Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jul 01, 2012
Posts: 11

Thanks!

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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Jul 01, 2012
Posts: 11

I got idea for using NetBeans and finnaly finished calculator

 
jQuery in Action, 2nd edition
 
subject: Problem with calculator