⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcengine.java

📁 现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为project的文件里.安装好后用bluej打开peoject的例子,可以进行你想要的任何变化.同时可以了解大量的源码
💻 JAVA
字号:
/** * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -