File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes another question on calculator applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another question on calculator applet" Watch "another question on calculator applet" New topic
Author

another question on calculator applet

Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
I am almost done with my applet. I have one last thing to do; I have to program it so that if the user hits the equal button, the calculator will return the last computed result. As it is, it is not doing that, it is repeating the operator function over and over again. I am not sure how to get it to display the result when the user hits the equal sign. Any advice would be helpful.

[This message has been edited by Cindy Glass (edited December 20, 2001).]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jade,
Your code works great for me! I'm running Win2000 and JDK1.2. When I hit the equals button the result appears in the textfield just as it should.
Good Work,
Manfred.
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
Yes, it is working, but our teacher wanted it to display the last result when we hit the equals button. I thought that meant the previous result? Maybe I misunderstood.
Anyway, thanks.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

'Previous' to me would be 'next to last,' as in "Show me the last step. Now show me the previous one."
If you change your point of reference at the right moment, they appear to be synonymous. This is where a broad liberal education, including lots of Looney Tunes re-runs, can be a big benefit:
Daffy Duck: Last week you said I could do my tholo this week!
Bugs Bunny: Right, Daffy. But this week last week was next week, not this week.
You gotta keep everything viewed from one place at one time, or you'll get dizzy.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
I think my education prior to this was too broad and liberal. I studied literary theory.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4746
    
    7

Would like some nitpicking of your code? There's a few ways it could be made shorter and better.
Junilu

Junilu - [How to Ask Questions] [How to Answer Questions]
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
Sure.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4746
    
    7

OK, you asked for it...
1. Something really nitpicky: Calculators normally have the number buttons arranged this way:
789
456
123
0
2. You can get rid of all the
if (source = btnXXX) { ... }
statements by factoring out the code in the if block to a separate object (hint: inner classes and MouseAdapter) that gets the label from the source and displays it etc.
3. You can do the same thing for the operator buttons as you do for the number buttons.
In general, try not to repeat yourself. If you find yourself writing the same code (with slight variations) more than twice, it's time to refactor.
Junilu
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
How do I refactor the if code out to a separate object? I don't know how to use inner classes.
I am very new to Java (like this is not obvious.)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4746
    
    7

OK, inner classes may be a little advanced for you at this point but it should be the next topic you'll have in class.
I'll give an example later...
Junilu
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
OK.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jade,
Since you are taking comments about the applet. I will just say that unless the exercise was to use a mouseListener that you should always try and make use of the objects that you are using. In your case an ActionListener would be more appropriate because all your objects are buttons.
Also you could make use of the buttons methods such as setActionCommand method and use getActionCommand method once inside your callback. It would get rid of much of your if/else blocks.
Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another question on calculator applet