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

calculator question

Chad Moore
Greenhorn

Joined: Mar 04, 2002
Posts: 7
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

Have you studied Finite State Machines yet?
Junilu
[ March 08, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Chad Moore
Greenhorn

Joined: Mar 04, 2002
Posts: 7
No ??
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

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

Joined: Dec 10, 2001
Posts: 7023
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


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Mar 04, 2002
Posts: 7
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calculator question