• 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

Problem with calculator

 
Greenhorn
Posts: 11
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!:$














 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

I'm bit confused now...but I will try to do something...
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got idea for using NetBeans and finnaly finished calculator

 
I am Arthur, King of the Britons. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic