/* * EqualButtonObj Class * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1997 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package Calculator; /** * This class is a smart button object that handles the Calculator's * equal sign. Every ButtonHandler should have one. * * @author: Lynn Andrea Stein, las@ai.mit.edu * @author: Luis F. G. Sarmenta, lfgs@mit.edu * @version: $$ * * @see Calculator, ButtonHandler, ButtonObj */ public class EqualButtonObj extends ButtonObj { /** * How to make one: Make a ButtonObj (i.e., my superclass). This * constructor exists only to call super w/the appropriate * arguments. * * @param gui The CalcTextGUI to keep track of. * @param stateObj The CalculatorState to keep track of. */ public EqualButtonObj( CalcTextGUI gui, CalculatorState stateObj ) { super( gui, stateObj ); } /** * This method is called by the ButtonHandler whenever the * decimal point button is pressed.

* * How to handle (my button) being pressed? If there's an * operation pending (but not yet done), do it to the remembered * previousOperand and the current operand (sitting on the * calculator's screen). The result goes on the calculator's * screen, there is no longer a pending operation, etc. */ public void handleButton() { // If we're handling =, we're not reading a # this.stateObj.doneReadingNumber(); // newOperand is what's on the screen, i.e., the currently // displayed value. double newOperand = ( this.gui.getValue() ); // The result of the operation is assigned to newValue. This is // the pending operation applied to the previous operand and the // new operand. The operation is smart enough to be able to do // the work itself, provided that we give it the two appropriate // operand arguments. double newValue = this.stateObj .getPendingOperation() .doOperation( this.stateObj.getPreviousOperand(), newOperand ); //phew! // Now, write the new value to the gui. this.gui.setText( String.valueOf( newValue ) ); // See NORMAL CASE, below. //This is some bookkeeping to handle divide-by-zero. //The screen can say whatever it wants, but we'd better make the //previous operand 0. if ( java.lang.Double.isInfinite( newValue ) ) { this.stateObj.setPreviousOperand( 0 ); } else { // This is the NORMAL CASE: remember the new value in case we // see an operator next (e.g., 2 + 3 = + 5 = should be 10). this.stateObj.setPreviousOperand( newValue ); } // Oh, yeah, and there's no more operation waiting to be done. this.stateObj.resetPendingOperation(); } } /* * $Log: EqualButtonObj.java,v $ * Revision 1.1.1.1 2002/06/05 21:56:25 root * CS101 comes to Olin finally. * * Revision 1.3 1997/10/30 03:28:36 lfgs * Added overview and fixed "* /" commenting typo in code. * * Revision 1.2 1997/10/24 22:20:41 las * Documented all of the code files, but didn't write the user manual (yet). * */