calcengine.java

来自「现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为p」· Java 代码 · 共 152 行

JAVA
152
字号
/** * The main part of the calculator doing the calculations. * This version incorporates debugging print statements.  * @author Hacker T. Largebrain (version 0.1) * @version 0.2 */public class CalcEngine{    // The value in the display.    private int displayValue;    // The previous operator typed (+ or -).    private char previousOperator;    // The left operand to previousOperator.    private int leftOperand;    /**     * Create a CalcEngine instance.     */    public CalcEngine()    {        displayValue = 0;        previousOperator = ' ';        leftOperand = 0;    }    /**     * Return the value currently displayed     * on the calculator.     */    public int getDisplayValue()    {        return displayValue;    }    /**     * A number button was pressed.     */    public void numberPressed(int number)    {        System.out.println("numberPressed called with: " +                           number);        displayValue = displayValue * 10 + number;        reportState("end of numberPressed");    }    /**     * The '+' button was pressed.      */    public void plus()    {        System.out.println("plus called");        applyPreviousOperator();        previousOperator = '+';        displayValue = 0;        reportState("end of plus");    }    /**     * The '-' button was pressed.     */    public void minus()    {        applyPreviousOperator();        previousOperator = '-';        displayValue = 0;    }        /**     * The '=' button was pressed.     */    public void equals()    {        System.out.println("equals called");        if(previousOperator == '+') {            displayValue = leftOperand + displayValue;        }        else {            displayValue = leftOperand - displayValue;        }        leftOperand = 0;        reportState("end of equals");    }    /**     * The 'C' (clear) button was pressed.     */    public void clear()    {        System.out.println("clear called");        displayValue = 0;        reportState("end of clear");    }    /**     * Return the title of this calculation engine.     */    public String getTitle()    {        return "Super Calculator";    }    /**     * Return the author of this engine.     */    public String getAuthor()    {        return "Hacker T. Largebrain";    }    /**     * Return the version number of this engine.     */    public String getVersion()    {        return "version 0.2";    }    /**     * Print the values of this object's fields.     * @param where Where this state occurs.     */    public void reportState(String where)    {        System.out.println("displayValue: " + displayValue +                           " leftOperand: " + leftOperand +                           " previousOperator: " +                           previousOperator + " at " + where);    }        /**     * An operator button has been pressed.     * Apply the immediately preceding operator to     * calculate an intermediate result. This will     * form the left operand of the new operator.     */    private void applyPreviousOperator()    {        System.out.println("applyPreviousOperator called");        if(previousOperator == '+') {            leftOperand += displayValue;        }        else if(previousOperator == '-') {            leftOperand -= displayValue;        }        else {            // There was no preceding operator.            leftOperand = displayValue;        }        reportState("end of applyPreviousOperator");    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?