Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calculator question

 
Chad Moore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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 ]
 
Junilu Lacar
Bartender
Posts: 7581
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
No ??
 
Junilu Lacar
Bartender
Posts: 7581
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • 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
  • 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
Bartender
Posts: 7581
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic