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:
- Am I (in the middle of) reading a number?
- Have I seen the decimal point yet?
- Have I seen an operation that I'll do (just as soon as I get its second
operand)?
- What was its first operand, anyway?
- See Also:
- Calculator
-
CalculatorState()
-
doneReadingNumber()
- Should be called whenever a non-digit (non-dot) is seen.
-
getPendingOperation()
- Accessor ("getter") method for pending operation.
-
getPreviousOperand()
- Accessor ("getter") method for previous operand.
-
justSawDot()
- Should be called when a dot is seen.
-
noDotYet()
- A test to see if we've seen a decimal place in the number we're
currently reading.
-
notReadingNumber()
- A test to see if state is in middle of reading number.
-
reset()
- Restores the CalculatorState to its pristine start state.
-
resetPendingOperation()
- Should be called when no operations are pending, e.g., after = or
reset.
-
setPendingOperation(OpButtonObj)
- Should be called whenever an operation is seen.
-
setPreviousOperand(double)
- Mutator ("setter") method for previous operand.
-
startReadingNumber()
- Should be called whenever a first digit is seen.
CalculatorState
public CalculatorState()
reset
public void reset()
- Restores the CalculatorState to its pristine start state.
- No pending operation.
- No previously seen operand (use 0).
- Haven't seen first digit.
- Haven't seen dot.
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.
startReadingNumber
public void startReadingNumber()
- Should be called whenever a first digit is seen. Updates state to
start composing numbers and a (single) decimal point.
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.
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.
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.
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.
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.
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.
getPendingOperation
public OpButtonObj getPendingOperation()
- Accessor ("getter") method for pending operation.
- Returns:
- s the currently pending operation (waiting to be done).
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.
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