This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
posted
0
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.
'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
posted
0
I think my education prior to this was too broad and liberal. I studied literary theory.
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
posted
0
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.)
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
posted
0
OK.
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
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.