Calculator


Your browser is ignoring the <APPLET> tag!

A basic calculator.

import objectdraw.*;

/**
 * A graphical user interface for the calculator. No calculation is being
 * done here. This class is responsible just for putting up the display on 
 * screen. It then refers to the "CalcEngine" to do all the real work.
 * 
 * @author David J. Barnes and Michael Kolling
 * @version 2006.03.30
 */
public class Calculator extends WindowController
{
    private static final int DISPLAY_LEFT = 20;
    private static final int DISPLAY_TOP = 20;
    private static final int DISPLAY_WIDTH = 360;
    private static final int DISPLAY_HEIGHT = 30;
    private static final int SPACING = 5;
    
    private ButtonCollection buttons;

    private CalcEngine calc;
    private boolean showingAuthor;

    private Text display;
    private Text status;

    /**
     * Create a user interface.
     * @param engine The calculator engine.
     */
    public void begin ()
    {
        calc = new CalcEngine();
        showingAuthor = true;
        makeFrame();
    }

    /**
     * Make the frame for the user interface.
     */
    private void makeFrame()
    {
        FramedRect displayRect = new FramedRect (DISPLAY_LEFT, DISPLAY_TOP, DISPLAY_WIDTH, DISPLAY_HEIGHT, canvas);
        display = new Text("", DISPLAY_LEFT, DISPLAY_TOP, canvas);
        display.setFontSize (30);
        buttons = new ButtonCollection(15);
        
        Button newButton = new Button (DISPLAY_LEFT, DISPLAY_TOP + DISPLAY_HEIGHT + SPACING, "7", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, DISPLAY_TOP + DISPLAY_HEIGHT + SPACING, "8", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, DISPLAY_TOP + DISPLAY_HEIGHT + SPACING, "9", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, DISPLAY_TOP + DISPLAY_HEIGHT + SPACING, "C", canvas);
        buttons.addButton (newButton);
        newButton = new Button (DISPLAY_LEFT, newButton.getBottom() + SPACING, "4", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "5", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "6", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "?", canvas);
        buttons.addButton (newButton);
        newButton = new Button (DISPLAY_LEFT, newButton.getBottom() + SPACING, "1", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "2", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "3", canvas);
        buttons.addButton (newButton);
        newButton = new Button (DISPLAY_LEFT, newButton.getBottom() + SPACING, "0", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "+", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "-", canvas);
        buttons.addButton (newButton);
        newButton = new Button (newButton.getRight() + SPACING, newButton.getTop(), "=", canvas);
        buttons.addButton (newButton);

        status = new Text(calc.getAuthor(), DISPLAY_LEFT, newButton.getBottom() + 3 * SPACING, canvas);
    }

    /**
     * An interface action has been performed.
     * Find out what it was and handle it.
     * @param event The event that has occured.
     */
    public void onMouseClick(Location point)
    {
        String command = buttons.getCommandAt(point);

        if(command.equals("0") ||
           command.equals("1") ||
           command.equals("2") ||
           command.equals("3") ||
           command.equals("4") ||
           command.equals("5") ||
           command.equals("6") ||
           command.equals("7") ||
           command.equals("8") ||
           command.equals("9")) {
            int number = Integer.parseInt(command);
            calc.numberPressed(number);
        }
        else if(command.equals("+")) {
            calc.plus();
        }
        else if(command.equals("-")) {
            calc.minus();
        }
        else if(command.equals("=")) {
            calc.equals();
        }
        else if(command.equals("C")) {
            calc.clear();
        }
        else if(command.equals("?")) {
            showInfo();
        }
        // else unknown command.

        redisplay();
    }

    /**
     * Update the interface display to show the current value of the 
     * calculator.
     */
    private void redisplay()
    {
        display.setText("" + calc.getDisplayValue());
    }

    /**
     * Toggle the info display in the calculator's status area between the
     * author and version information.
     */
    private void showInfo()
    {
        if(showingAuthor)
            status.setText(calc.getVersion());
        else
            status.setText(calc.getAuthor());

        showingAuthor = !showingAuthor;
    }
}

/**
 * The main part of the calculator doing the calculations.
 * @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 " + number);
        if (previousOperator == '+' || previousOperator == '-') {
            displayValue = number;
        }
        else if (previousOperator == '=') {
            clear();
            displayValue = number;
        }
        else {
            displayValue = displayValue * 10 + number;
        }

        System.out.println ("displayValue = " + displayValue);
    }

    /**
     * The '+' button was pressed. 
     */
    public void plus()
    {
        applyPreviousOperator();
        previousOperator = '+';
       // displayValue = 0;
    }

    /**
     * The '-' button was pressed.
     */
    public void minus()
    {
        applyPreviousOperator();
        previousOperator = '-';
      //  displayValue = 0;
    }
    
    /**
     * The '=' button was pressed.
     */
    public void equals()
    {
        if(previousOperator == '+') {
            displayValue = leftOperand + displayValue;
        }
        else {
            displayValue = leftOperand - displayValue;
        }
        leftOperand = 0;
        previousOperator = '=';
    }

    /**
     * The 'C' (clear) button was pressed.
     */
    public void clear()
    {
        displayValue = 0;
        previousOperator = ' ';
        leftOperand = 0;
    }

    /**
     * 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";
    }

    /**
     * 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()
    {
        if(previousOperator == '+') {
            leftOperand += displayValue;
        }
        else if(previousOperator == '-') {
            leftOperand -= displayValue;
        }
        else {
            // There was no preceding operator.
            leftOperand = displayValue;
        }
    }
}

import objectdraw.Location;

/**
 * This class manages a collection of buttons
 * 
 * @author Barbara Lerner
 * @version April 2008
 */
public class ButtonCollection
{
    // The buttons
    private Button[] buttons;
    
    // The next empty slot in the array
    private int nextButton = 0;
    
    /**
     * Create an empty collection 
     * @param numButtons the number of buttons the collection should be able to hold
     */
    public ButtonCollection (int numButtons) {
        buttons = new Button[numButtons];
    }
    
    /**
     * Add a button to the collection
     * @param b the button to add.
     */
    public void addButton (Button b) {
        buttons[nextButton] = b;
        nextButton++;
    }
    
    /**
     * Find the button that the user clicked on.  Return null if the user did not click
     * on a button.
     * @param point where the user clicked.
     */
    public String getCommandAt (Location point) {
        // Search the array
        for (int i = 0; i < buttons.length; i++) {
            // Make sure there is actually a button in this slot of the array.  If there
            // is, see if the user clicked on it.
            if (buttons[i] != null && buttons[i].contains (point)) {
                return buttons[i].getCommand();
            }
        }
        return null;
    }

}

import objectdraw.*;

/**
 * A labeled rectangle that the user can click on.  It looks
 * like a simple button.
 * 
 * @author Barbara Lerner 
 * @version March 25, 2008
 */
public class Button
{
    private static final int BUTTON_INSET = 3;
    private static final int BUTTON_WIDTH = 85;
    private static final int BUTTON_HEIGHT = 70;
    private FramedRect box;
    private Text text;
    
    public Button (double left, double top, String label, DrawingCanvas canvas) {
        text = new Text (label, 0, 0, canvas);
        text.setFontSize (40);
        double textWidth = text.getWidth();
        double textHeight = text.getHeight();
        box = new FramedRect (left, top, BUTTON_WIDTH, BUTTON_HEIGHT, canvas);
        text.moveTo (left + (BUTTON_WIDTH - textWidth) / 2, top + (BUTTON_HEIGHT - textHeight) / 2 );
    }
    
    public boolean contains (Location point) {
        return box.contains (point);
    }
    
    public double getRight() {
        return box.getX() + box.getWidth();
    }
    
    public double getTop() {
        return box.getY();
    }
    
    public double getBottom() {
        return box.getY() + box.getHeight();
    }
    
    public String getCommand() {
        return text.getText();
    }
}