/** Calculator.java a simple Calculator applet @author Jay Tomlin @version 1.0 http://ezinfo.ucs.indiana.edu/~ltomlin/ Bloomington, Indiana, USA December 1996 */ import java.applet.*; import java.awt.*; public class Calculator extends Applet { // // Declare the class variables // double x, y; Button Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine; Button Plus, Minus, Times, DividedBy, Sin, Cos, Tan, Log, Exp, Ran; Button Clear, ClearEntry, Decimal, Equals; Panel ButtonPanel, DisplayPanel; TextField Display; GridLayout grid1, grid2; BorderLayout border; boolean HasDecimal, ReplaceDisplay; String CurrentFunction, FirstNumber; public void init() { // // Size it to a nice Golden-ratio proportion // resize(120,174); // // Create the component objects // Display = new TextField(); // left blank so it stretches to fill panel Display.setEditable(false); Zero = new Button("0"); One = new Button("1"); Two = new Button("2"); Three = new Button("3"); Four = new Button("4"); Five = new Button("5"); Six = new Button("6"); Seven = new Button("7"); Eight = new Button("8"); Nine = new Button("9"); Plus = new Button("+"); Minus = new Button("-"); Times = new Button("*"); DividedBy = new Button("/"); Sin = new Button("sin"); Cos = new Button("cos"); Tan = new Button("tan"); Log = new Button("ln"); Exp = new Button("e"); Ran = new Button("ran"); Clear = new Button("C"); ClearEntry = new Button("CE"); Decimal = new Button("."); Equals = new Button("="); ButtonPanel = new Panel(); DisplayPanel = new Panel(); // // 24 buttons go on a 6x4 grid // grid1 = new GridLayout(6, 4); ButtonPanel.setLayout(grid1); // and the Display gets a Panel of its own grid2 = new GridLayout(1, 1); DisplayPanel.setLayout(grid2); // // Add the elements to their respective Panels // DisplayPanel.add(Display); ButtonPanel.add(Ran); ButtonPanel.add(Exp); ButtonPanel.add(Log); ButtonPanel.add(Clear); ButtonPanel.add(Sin); ButtonPanel.add(Cos); ButtonPanel.add(Tan); ButtonPanel.add(ClearEntry); ButtonPanel.add(Seven); ButtonPanel.add(Eight); ButtonPanel.add(Nine); ButtonPanel.add(DividedBy); ButtonPanel.add(Four); ButtonPanel.add(Five); ButtonPanel.add(Six); ButtonPanel.add(Times); ButtonPanel.add(One); ButtonPanel.add(Two); ButtonPanel.add(Three); ButtonPanel.add(Minus); ButtonPanel.add(Zero); ButtonPanel.add(Decimal); ButtonPanel.add(Equals); ButtonPanel.add(Plus); // // Finally, add and show the Panels // border = new BorderLayout(); this.setLayout(border); this.add("North", DisplayPanel); this.add("Center", ButtonPanel); this.show(); } public void start() { ReplaceDisplay = true; FirstNumber = "0"; CurrentFunction = "nothing"; HasDecimal = false; Display.setText("0"); x = 0; y = 0; } // // Handle user clicks. // Choices are: it was a number, it was a unary operator, // or it was a binary operator // public boolean handleEvent(Event e) { switch(e.id) { case Event.ACTION_EVENT : { // button clicks fall into this category if (e.target == Zero) numberClicked((String)e.arg); if (e.target == One) numberClicked((String)e.arg); if (e.target == Two) numberClicked((String)e.arg); if (e.target == Three) numberClicked((String)e.arg); if (e.target == Four) numberClicked((String)e.arg); if (e.target == Five) numberClicked((String)e.arg); if (e.target == Six) numberClicked((String)e.arg); if (e.target == Seven) numberClicked((String)e.arg); if (e.target == Eight) numberClicked((String)e.arg); if (e.target == Nine) numberClicked((String)e.arg); if (e.target == Decimal) {if(HasDecimal) {;} //do nothing else { HasDecimal = true; numberClicked((String)e.arg);}} if (e.target == Plus) binaryFunctionClicked((String)e.arg); if (e.target == Minus) binaryFunctionClicked((String)e.arg); if (e.target == Times) binaryFunctionClicked((String)e.arg); if (e.target == DividedBy) binaryFunctionClicked((String)e.arg); if (e.target == Sin) unaryFunctionClicked((String)e.arg); if (e.target == Cos) unaryFunctionClicked((String)e.arg); if (e.target == Tan) unaryFunctionClicked((String)e.arg); if (e.target == Log) unaryFunctionClicked((String)e.arg); if (e.target == Exp) unaryFunctionClicked((String)e.arg); if (e.target == Ran) unaryFunctionClicked((String)e.arg); if (e.target == Clear) unaryFunctionClicked((String)e.arg); if (e.target == ClearEntry) { Display.setText("0"); HasDecimal = false; ReplaceDisplay = true; } if (e.target == Equals) { String SecondNumber = new String(Display.getText()); equalsClicked(FirstNumber, CurrentFunction, SecondNumber); } } // ends case Event.ACTION_EVENT default: } // ends switch(e.id) return true; } // ends handleEvent() public void numberClicked(String argument) { String currentNumber = Display.getText(); if(ReplaceDisplay) { if(argument.equals(".")) { Display.setText("0.");} else { Display.setText(argument); } // ends if(argument.equals(".") ReplaceDisplay = false; } else { Display.setText(currentNumber + argument); } // ends if(ReplaceDisplay) } // ends numberClicked() public void unaryFunctionClicked(String argument) { String currentDisplay = Display.getText(); try { Double X = new Double(currentDisplay); x = X.doubleValue(); } catch (NumberFormatException e) { x = 0; } if (argument.equals("sin")) y = Math.sin(x); if (argument.equals("cos")) y = Math.cos(x); if (argument.equals("tan")) y = Math.tan(x); if (argument.equals("ln")) y = Math.log(x); if (argument.equals("e")) y = Math.exp(x); if (argument.equals("ran")) y = Math.random(); if (argument.equals("C")) { x = 0; y = 0; HasDecimal = false; FirstNumber = "0"; CurrentFunction = "nothing"; ReplaceDisplay = true; } Double Answer = new Double(y); Display.setText(Answer.toString()); ReplaceDisplay = true; HasDecimal = false; } // ends unaryFunctionClicked public void binaryFunctionClicked(String argument) { equalsClicked(FirstNumber, CurrentFunction, Display.getText()); CurrentFunction = argument; ReplaceDisplay = true; } // ends binaryFunctionClicked public void equalsClicked(String numberOne, String function, String numberTwo) { try { Double X = new Double(numberOne); x = X.doubleValue(); } catch (NumberFormatException e) { x = 0; } try { Double Y = new Double(numberTwo); y = Y.doubleValue(); } catch (NumberFormatException e) { y = 0; } // Now x and y should be valid doubles, ready for x + y and the like. double z = 0; // the result if (function.equals("+")) z = x + y; if (function.equals("-")) z = x - y; if (function.equals("*")) z = x * y; if (function.equals("/")) z = x / y; if (function.equals("nothing")) z = y; Double Answer = new Double(z); Display.setText(Answer.toString()); FirstNumber = Answer.toString(); ReplaceDisplay = true; HasDecimal = false; CurrentFunction = "nothing"; } // ends equalsClicked } // ends class Calculator