Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another question on calculator applet

 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my education prior to this was too broad and liberal. I studied literary theory.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would like some nitpicking of your code? There's a few ways it could be made shorter and better.
Junilu
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure.
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic