/* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-565755-5 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.00 07 Feb 1996 * @author Cay Horstmann */ import java.awt.*; import corejava.*; public class Calculator extends Frame { public Calculator() { setTitle("Calculator"); display = new TextField("0"); display.setEditable(false); add("North", display); Panel p = new Panel(); p.setLayout(new GridLayout(4, 4)); for (int i = 0; i <= 9; i++) p.add(new Button("" + (char)('0' + i))); p.add(new Button("+")); p.add(new Button("-")); p.add(new Button("*")); p.add(new Button("/")); p.add(new Button("%")); p.add(new Button("=")); add("Center", p); } public boolean handleEvent(Event evt) { if (evt.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { if (arg instanceof String) { String s = (String) arg; if ('0' <= s.charAt(0) && s.charAt(0) <= '9') { if (start) display.setText(s); else display.setText(display.getText() + s); start = false; } else { if (start) { if (s.equals("-")) { display.setText(s); start = false; } else op = s; } else { calculate(Format.atoi(display.getText())); op = s; start = true; } } } else return super.action(evt, arg); return true; } public void calculate(int n) { if (op == "+") arg += n; else if (op == "-") arg -= n; else if (op == "*") arg *= n; else if (op == "/") arg /= n; else if (op == "%") arg %= n; else if (op == "=") arg = n; display.setText("" + arg); } public static void main(String[] args) { Frame f = new Calculator(); f.resize(300, 200); f.show(); } private TextField display; private int arg = 0; private String op = "="; private boolean start = true; }