All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class Calculator.CalculatorState

java.lang.Object
   |
   +----Calculator.CalculatorState

public class CalculatorState
extends Object
This class holds most of the state information for a Calculator. Every ButtonHandler should have exactly one. When it is created, the Calculator's state is "reset": it hasn't seen any numbers, operations, etc. (It would actually be reasonable for it to have seen a 0, but this isn't implemented.) It records such things as:

See Also:
Calculator

Constructor Index

 o CalculatorState()

Method Index

 o doneReadingNumber()
Should be called whenever a non-digit (non-dot) is seen.
 o getPendingOperation()
Accessor ("getter") method for pending operation.
 o getPreviousOperand()
Accessor ("getter") method for previous operand.
 o justSawDot()
Should be called when a dot is seen.
 o noDotYet()
A test to see if we've seen a decimal place in the number we're currently reading.
 o notReadingNumber()
A test to see if state is in middle of reading number.
 o reset()
Restores the CalculatorState to its pristine start state.
 o resetPendingOperation()
Should be called when no operations are pending, e.g., after = or reset.
 o setPendingOperation(OpButtonObj)
Should be called whenever an operation is seen.
 o setPreviousOperand(double)
Mutator ("setter") method for previous operand.
 o startReadingNumber()
Should be called whenever a first digit is seen.

Constructors

 o CalculatorState
 public CalculatorState()

Methods

 o reset
 public void reset()
Restores the CalculatorState to its pristine start state. Aesthetic note: this should really be called by the constructor instead of initializing each field separately. That way, "default" is defined in a single place.

 o startReadingNumber
 public void startReadingNumber()
Should be called whenever a first digit is seen. Updates state to start composing numbers and a (single) decimal point.

 o doneReadingNumber
 public void doneReadingNumber()
Should be called whenever a non-digit (non-dot) is seen. Updates state to reflect that number is done, i.e., new digit starts new number.

 o notReadingNumber
 public boolean notReadingNumber()
A test to see if state is in middle of reading number.

Returns:
s false if called when in middle of reading number, true otherwise.
 o noDotYet
 public boolean noDotYet()
A test to see if we've seen a decimal place in the number we're currently reading.

Returns:
s true if called after a decimal place has been seen in this number, false if number is integral.
 o justSawDot
 public void justSawDot()
Should be called when a dot is seen. Modifies state to indicate that a decimal place has already been seen in this number.

 o setPendingOperation
 public void setPendingOperation(OpButtonObj currOp)
Should be called whenever an operation is seen. Records that operation for future use (after second operand is seen).

Parameters:
currOp - the operation button seen.
 o resetPendingOperation
 public void resetPendingOperation()
Should be called when no operations are pending, e.g., after = or reset. Makes pendingOperation NOOP.

This should be the sole place that this default is set, so really reset() should call this method. That way, if we ever want to change the Calculator's default state, this is the one place we'd have to make the change.

 o getPendingOperation
 public OpButtonObj getPendingOperation()
Accessor ("getter") method for pending operation.

Returns:
s the currently pending operation (waiting to be done).
 o setPreviousOperand
 public void setPreviousOperand(double num)
Mutator ("setter") method for previous operand. Remembers this operand until after the operation, second operand are available and the result can be computed.

Parameters:
num - the operand to remember.
 o getPreviousOperand
 public double getPreviousOperand()
Accessor ("getter") method for previous operand.

Returns:
s the remembered left-hand operand (waiting to be operated upon).

All Packages  Class Hierarchy  This Package  Previous  Next  Index