/** * CS1312 Lab #17 - The Model. *
 *  Calculator.java - A basic calculator.
 *
 *  Revisions: 1.1 Mar. 14, 2001
 *               Added minor modifications to calculation behavior.  Added
 *               some documentation.
 *             1.0 Jan. 11, 2001
 *               Created the Calculator class
 *
 *  
* * @author Luke A. Olbrish * @version Version 1.1, Mar. 14, 2001 */ public class Calculator { // Constants /** * This constant denotes the use of the clear operation. */ public static final int CLR_OP = 0; /** * This constant denotes the computation operation. */ public static final int EQL_OP = 1; /** * This constant denotes that addition should be used. * * @see #DIV_OP * @see #MUL_OP * @see #SUB_OP */ public static final int ADD_OP = 2; /** * This constant denotes that subtraction should be used. * * @see #ADD_OP * @see #DIV_OP * @see #MUL_OP */ public static final int SUB_OP = 3; /** * This constant denotes that multiplication should be used. * * @see #ADD_OP * @see #DIV_OP * @see #SUB_OP */ public static final int MUL_OP = 4; /** * This constant denotes that division should be used. * * @see #ADD_OP * @see #MUL_OP * @see #SUB_OP */ public static final int DIV_OP = 5; // Data Block /** * Status variable that determines if operator1 is in an initialized form. * * @see #isOperator2Set * @see #operator1 */ private boolean isOperator1Set = false; /** * The value of the first operator. * * @see #isOperator1Set * @see #operator2 */ private int operator1 = Integer.MIN_VALUE; /** * Status variable that determines if operator2 is in an initialized form. * * @see #isOperator1Set * @see #operator2 */ private boolean isOperator2Set = false; /** * The value of the second operator. * * @see #isOperator2Set * @see #operator1 */ private int operator2 = Integer.MIN_VALUE; /** * Status variable that determines if the operation is in an initialized * form. * * @see #operation */ private boolean isOperationSet = false; /** * The value of the math operation. * * @see #isOperationSet */ private int operation = Integer.MIN_VALUE; // Accessors /** * Reports whether the operation has been set yet. * * @see #isOperator1Set * @see #isOperator2Set */ public boolean isOperationSet() { return this.isOperationSet; }// end of isOperationSet() /** * Reports whether operation 1 has been set yet. * * @see #isOperationSet * @see #isOperator2Set */ public boolean isOperator1Set() { return this.isOperator1Set; }// end of isOperator1Set() /** * Reports whether operation 2 has been set yet. * * @see #isOperationSet * @see #isOperator1Set */ public boolean isOperator2Set() { return this.isOperator2Set; }// end of isOperator2Set() /** * Gets the calculation of the operators and operation if possible. If * only operator1 is initialized, then operator1 is returned. If * non of the operators or operation are initialized than an Arithmetic * Exception is thrown. *

* * @exception ArithmeticException * @return an integer representing the calculation of the operators and the * operation. */ public int getCalculation() throws ArithmeticException { int returnVal = Integer.MIN_VALUE; // check to see if operators and operation are initialized. if(( !isOperationSet ) || ( !isOperator2Set )) { if( isOperator1Set ) return operator1; else throw new ArithmeticException( "Not Enough Information" ); }// end of if // Do the proper operation based upon the operation constant. switch( operation ) { case ADD_OP: returnVal = operator1 + operator2; break; case SUB_OP: returnVal = operator1 - operator2; break; case MUL_OP: returnVal = operator1 * operator2; break; case DIV_OP: returnVal = operator1 / operator2; break; default: throw new ArithmeticException( "Not Enough Information" ); }// end of switch( int ) // reset the data isOperator1Set = false; operator1 = Integer.MIN_VALUE; isOperator2Set = false; operator2 = Integer.MIN_VALUE; isOperationSet = false; operation = 0; return returnVal; }// end of getCalculation() // Modifiers /** * Sets the first operator. This function will also set a flag that * indicates that the operator has a valid value. *

* * @param operator1 the new value of the first operator */ public void setOperator1( int operator1 ) { this.isOperator1Set = true; this.operator1 = operator1; }// end of setOperator1( int ) /** * Sets the second operator. This function will also set a flag that * indicates that the operator has a valid value. *

* * @param operator2 the new value of the second operator */ public void setOperator2( int operator2 ) { this.isOperator2Set = true; this.operator2 = operator2; }// end of setOperator2( int ) /** * Sets the math operation. This function will also set a flag that * indicates that the operation has a valid value. *

* * @param operation the new value of the operation */ public void setOperation( int operation ) { if(( !isOperator1Set ) || (( this.operation >= ADD_OP ) && ( this.operation <= DIV_OP ))) return; this.isOperationSet = true; this.operation = operation; }// end of setOperation( int ) // Operational Functions /** * This function clears all information from the calculator. It will reset * the calculator to the state that it was when it was first created. *

*/ public void clearCalulator() { // Reset all information back to the default state. isOperator1Set = false; operator1 = Integer.MIN_VALUE; isOperator2Set = false; operator2 = Integer.MIN_VALUE; isOperationSet = false; operation = 0; }// end of clearCalculator }// end of class Calculator