Java: Calculation Class

Calculation Class

Description

Provides a mechanism to hold value of previous calculations, and then make new calculations with this value.

The way this class works is to hold value representing the current value of the calculator and a value repesenting the next operation (addition, multiplication etc) that needs to take place. Subsequent calls to public double calculate( double newValue, char newOperation ) result in the current calculator value being adjusted and the next operation value being replaced with the next new one.

Source

[css]
/*
 * Calculation
 *
 * This class receives numbers and operators and performs running calculations
 * Includes methods for accessing the state of the receiver
 *
 * Russell Smithers - 28th October 2006
 *
 */

import java.util.*;

public class Calculation
{

    private double currentValue; // Current value of calculation
    private char nextOperation;  // Operation to carry out when next value received

    /**
     * Constructor
     */
    public Calculation()
    {
        this.currentValue = 0.0; this.nextOperation = '+';
    }

    /**
     * Calculates and sets a new value for the receiver based upon the receivers next oparator
     * its current value, and the value passed in, and then sets the next operation of the
     * receiver to the operation passed in.
     */
    public double calculate( double newValue, char newOperation )
    {
        double result;
        char op = this.getOperation();
        result = Calculation.calculate(this.getValue(), this.getOperation(), newValue);
        this.setValue( result );
        this.setOperation( newOperation );
        return result;
    }

    /**
     * Performs a calculation on an (number operator number)
     */
    public static double calculate( double leftSide, char howCalc, double rightSide )
    {
        double result=0.0;
        try
        {
            //result = leftSide + howCalc + rightSide;
            switch(howCalc)
            {
                case '+': result = leftSide + rightSide; break;
                case '-': result = leftSide - rightSide; break;
                case '*': result = leftSide * rightSide; break;
                case '/': result = leftSide / rightSide; break;
            }

        }
        catch(Exception anError)
        {
            System.out.println("Calculation.calculate() - " + leftSide + " " + howCalc + rightSide + ": " +anError);
        }
        System.out.println("calculate(double, char, double) " + result );
        return result;
    }

    /**
     * Changes the receiver to be the result of a calculation between itself and newCalc
     * The calculation is the same as; public void calculate( double newValue, char newOperation )
     */
    public double calculate( Calculation newCalc )
    {
        return this.calculate( newCalc.getValue(), newCalc.getOperation() );
    }

    /**
     * Returns true if the operation to be tested is one that this Calculation can deal with
     */
    public static boolean operationValid(char testOperation)
    {
        boolean valid;
        switch(testOperation)
        {
            case '+':
            case '-':
            case '*':
            case '/':
            case '=':
                valid=true;
                break;
            default: valid=false;
        }
        return valid;
    }

    /**
     * Sets the current value of the receiver
     */
    public void setValue(double newValue)
    { this.currentValue = newValue; }

    /**
     * Sets the nextOperation of the receiver
     * returns true if succesfull
     */
    public boolean setOperation( char newOperation )
    {
        if ( Calculation.operationValid( newOperation ) )
        {
            // If operation is equals set it to plus
            this.nextOperation = ( newOperation == '=' ? '+' : newOperation);
            return true;
        }
        return false;
    }

    /**
     * Returns the current value of the receiver
     */
    public double getValue()   { return this.currentValue; }

    /**
     * Returns the nextOperation of the receiver
     */
    public char getOperation() { return this.nextOperation; }

    /**
     * Returns a string representation of the receiver
     */
    public String toString()
    {
        return "The current value is " + currentValue + " and the next operation to be performed is " + nextOperation;
    }

}
[/css]

Leave a Reply