.
*
* 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
* operations (+, -, *, /). Every ButtonHandler should have some.
*
* @author: Lynn Andrea Stein, las@ai.mit.edu
* @author: Luis F. G. Sarmenta, lfgs@mit.edu
* @version: $$
*
* @see Calculator, ButtonHandler, ButtonObj
*/
public class OpButtonObj extends ButtonObj
{
/**
* The one and only NOOP button. It exists mostly to not be one
* of the other objects, so the most important thing about it is
* that it isn't one of the others. However, it isn't null,
* either, in case its handleButton ever gets called. (If its
* handleButton does get called, it doesn't do anything. Kind-of
* like a NOOP, no?)
*
* Initialized here, can't change. Note: static, so it belongs
* to the class: OpButtonObj.NO_OP_BUTTON_OBJ. It IS an instance
* of the class, though.
*/
public static final OpButtonObj NO_OP_BUTTON_OBJ
= new OpButtonObj( Calculator.NO_OP );
/**
* Which operation am I? This is one of Calculator's buttonIDs.
* Initialized by the cosntructor, doesn't change, but each one is
* different.
*/
private int buttonOp;
/**
* It turns out to be awfully handy to have this calculator's
* equals button around to tie up any pending operations. (If you
* get 2 + 3 + 4, you just pretend that you got 2 + 3 = and then
* the + 4 with the resulting 5.) So hold on to that equals
* ButtonObj!
*
* Initialized by constructor, doesn't change.
*/
private ButtonObj equalButton;
/**
* This constructor is private and should only be called from
* within this class. It is used to make the NOOP button. It
* should never be used otherwise.
* 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.
*/
private OpButtonObj( int buttonID )
{
super( null, null );
this.buttonOp = buttonID;
}
/**
* How to make one: Make a ButtonObj (i.e., my superclass) and
* also remember which operation I am (Calculator.OP_ADD, etc.),
* and always remember that equals button!
*
* @param op Which buttonID for whichever operation I am.
* @param equalButton The equal ButtonObj for this calculator.
* @param gui The CalcTextGUI to keep track of.
* @param stateObj The CalculatorState to keep track of.
*/
public OpButtonObj( int op,
ButtonObj equalButton,
CalcTextGUI gui,
CalculatorState stateObj )
{
super( gui, stateObj );
this.buttonOp = op;
this.equalButton = equalButton;
}
/**
* This method is called by the ButtonHandler whenever the
* decimal point button is pressed.
*
* How to handle (my button) being pressed?
*
* - We're not reading a number any more. Notify state.
*
- If there's already a pending operation, resolve it
* by pretending that the = button was pressed.
*
- Remember the number that will (eventually) be the
* left-hand operand to this operation.
*
*/
public void handleButton()
{
// Can't be reading a number if we see +-*/
this.stateObj.doneReadingNumber();
// Resolve any pending operations, e.g., if we've seen 2 + 3 +,
// calculate the 5.
if ( this.stateObj.getPendingOperation() != OpButtonObj.NO_OP_BUTTON_OBJ )
{
this.equalButton.handleButton();
} else {
this.stateObj.setPreviousOperand( this.gui.getValue() );
}
// And remember this operation to do it later.
this.stateObj.setPendingOperation( this );
}
/**
* This method is called when it is actually time to calculate the
* result of this operation (i.e., by = button). It does the
* appropriate numeric calculation on its operands and returns the
* result, i.e., leftOperand + - * / rightOperand as appropriate.
*
* @param leftOperand
* @param rightOperand
*/
public double doOperation ( double leftOperand, double rightOperand )
{
switch ( this.buttonOp )
{
case Calculator.OP_MUL:
return ( leftOperand * rightOperand );
case Calculator.OP_DIV:
return ( leftOperand / rightOperand );
case Calculator.OP_SUB:
return ( leftOperand - rightOperand );
case Calculator.OP_ADD:
return ( leftOperand + rightOperand );
case Calculator.NO_OP:
// fall through....
default:
return ( rightOperand );
}
}
}
/*
* $Log: OpButtonObj.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:37 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).
*
*/