• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

calculator question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me how to cascade my calculator functions.
What I mean is:
If the user clicks 1 + 2 + 3 the output should be displayed as 6.
I have to press 1 + 2 = 3 + 3 to display 6.
Otherwise it will display 5 (the last two operands added).
Here is my source code.

[ March 07, 2002: Message edited by: Chad Moore ]
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you studied Finite State Machines yet?
Junilu
[ March 08, 2002: Message edited by: Junilu Lacar ]
 
Chad Moore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No ??
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No ??
The reason I asked is because a Calculator is normally implemented as a Finite State Machine. Other topics that would be good for you to know about are stacks and Reverse Polish Notation. This is obviously coursework so I was just wondering if FSMs and RPN is something that you have already encountered in your studies.
There are quite a number of things you can do to improve your program. I know this is not what you asked originally but IMO it would be well worth the effort to refactor your design before adding new functionality. When you do get around to adding new functionality you can do it easily and naturally.
Do you want to give it a shot? I'd be happy to step you through. But if you'd rather just get the lab work done, I'm sure others could suggest a quicker fix.
Junilu
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm salivating at the prospect of learning some great design concepts. Chad, please accept the offer from Junilu or I'll have to post some really sloppy code to try and lure critique.
Good Luck,
-Dirk Schreckmann
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chad,
I can get you somewhere near (about 90%) using your existing code. I would just do the following three things:
1. Dump your result variable
2. Assign variable op1 Double.MAX_VALUE as a default and whenever we want to clear it.
3. Make better use of equalPressed boolean. You can use it for any operation pressed.
The code below gets really close.

Enjoy,
Manfred.
 
Chad Moore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred, thanks for taking the time to look at and alter my code. Junilu, as Dirk stated, I would like to learn some programming concepts that will give me more programming skills. The calculator was already turned in, but I would still like to work on it more. It was a quite a learning experience, so I think it would help even more to continue with it. Being new to Java and OOP for that matter, I will take all the help I can get.
Thanks again
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! I'll start a thread in OO, Patterns, and Refactoring tonight so check it out there.
Junilu
[ March 09, 2002: Message edited by: Junilu Lacar ]
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic