/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 26/10/98 Description: Part of a simple calculator applet based on a program in G.W. Rouse's An Introduction to Data Structures and Algorithms with Java, Prentice Hall, Hemel Hemstead, 1998. pp 65-78. */ import java.applet.Applet; import java.awt.*; import NewGridBagLayout; public class Calculator extends Applet{ private long operand1, operand2, answer; private String operation; private boolean newNumber; private Button button1 = new Button("1"); private Button button2 = new Button("2"); private Button button3 = new Button("3"); private Button button4 = new Button("4"); private Button button5 = new Button("5"); private Button button6 = new Button("6"); private Button button7 = new Button("7"); private Button button8 = new Button("8"); private Button button9 = new Button("9"); private Button button0 = new Button("0"); private TextField numberBox = new TextField("0", 12); private Button plusMinusButton = new Button("+/-"); private Button CEButton = new Button("CE"); private Button CButton = new Button("C"); private Button plusButton = new Button("+"); private Button minusButton = new Button("-"); private Button timesButton = new Button("*"); private Button divideButton = new Button("/"); private Button equalsButton = new Button("="); public void init() { Panel numberPanel = new Panel(); Panel signClearPanel = new Panel(); Panel arithmeticPanel = new Panel(); Panel masterPanel = new Panel(); NewGridBagLayout numberLayout = new NewGridBagLayout(); NewGridBagLayout masterLayout = new NewGridBagLayout(); operand1 = operand2 = answer = 0; operation = "+"; setFont(new Font("Helvetica", Font.BOLD, 14)); signClearPanel.setLayout(new GridLayout(1,3,2,2)); signClearPanel.add(plusMinusButton); signClearPanel.add(CEButton); signClearPanel.add(CButton); numberPanel.setLayout(numberLayout); numberLayout.setRareConstraints( GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2,2,2,2), 0, 0, 1, 1); numberLayout.setPositionSizeAdd(button1, numberPanel, 0, 0, 1, 1); numberLayout.setPositionSizeAdd(button2, numberPanel, 1, 0, 1, 1); numberLayout.setPositionSizeAdd(button3, numberPanel, 2, 0, 1, 1); numberLayout.setPositionSizeAdd(button4, numberPanel, 0, 1, 1, 1); numberLayout.setPositionSizeAdd(button5, numberPanel, 1, 1, 1, 1); numberLayout.setPositionSizeAdd(button6, numberPanel, 2, 1, 1, 1); numberLayout.setPositionSizeAdd(button7, numberPanel, 0, 2, 1, 1); numberLayout.setPositionSizeAdd(button8, numberPanel, 1, 2, 1, 1); numberLayout.setPositionSizeAdd(button9, numberPanel, 2, 2, 1, 1); numberLayout.setPositionSizeAdd(button0, numberPanel, 1, 3, 1, 1); arithmeticPanel.setLayout(new GridLayout(5,1,2,2)); arithmeticPanel.add(plusButton); arithmeticPanel.add(minusButton); arithmeticPanel.add(divideButton); arithmeticPanel.add(timesButton); arithmeticPanel.add(equalsButton); masterLayout.setRareConstraints( GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0, 1, 1); masterPanel.setLayout(masterLayout); masterLayout.setPositionSizeAdd(numberBox, masterPanel, 0, 0, 2, 1); masterLayout.setPositionSizeAdd(signClearPanel, masterPanel, 0, 1, 2, 1); masterLayout.setPositionSizeAdd(numberPanel, masterPanel, 0, 2, 1, 1); masterLayout.setPositionSizeAdd(arithmeticPanel, masterPanel, 1, 2, 1, 1); add(masterPanel); } public boolean action(Event event, Object arg){ if (event.target instanceof Button){ String label = (String)arg; if(event.target== button1){ addDigitToDisplay(1); } else if(event.target== button2){ addDigitToDisplay(2); } else if(event.target== button3){ addDigitToDisplay(3); } else if(event.target== button4){ addDigitToDisplay(4); } else if(event.target== button5){ addDigitToDisplay(5); } else if(event.target== button6){ addDigitToDisplay(6); } else if(event.target== button7){ addDigitToDisplay(7); } else if(event.target== button8){ addDigitToDisplay(8); } else if(event.target== button9){ addDigitToDisplay(9); } else if(event.target== button0){ addDigitToDisplay(0); } else if(event.target== CEButton){ clearDisplay(); } else if(event.target== plusMinusButton){ changeSign(); } else if(event.target== CButton){ clearAll(); } else if(event.target== equalsButton){ doEquals(); } else if(event.target== plusButton){ doOperation("+"); } else if(event.target== minusButton){ doOperation("-"); } else if(event.target== timesButton){ doOperation("*"); } else if(event.target== divideButton){ doOperation("/"); } return true; } return super.action(event, arg); } private void addDigitToDisplay(int newDigit) { if(newNumber){ numberBox.setText("0"); newNumber = false; } long displayValue = Long.parseLong(numberBox.getText()); if (displayValue >= 0 && displayValue <= 999999999) displayValue = displayValue*10 + newDigit; numberBox.setText(Long.toString(displayValue)); } private void changeSign() { long display = Long.parseLong(numberBox.getText()); display = -display; numberBox.setText(Long.toString(display)); } private void clearDisplay() { numberBox.setText("0"); } private void clearAll() { operand1 = operand2 = answer = 0; operation = "+"; newNumber = true; numberBox.setText("0"); } private void doEquals() { if(newNumber) return; operand2 = Long.parseLong(numberBox.getText()); if (operation == "+") answer = operand1 + operand2; else if (operation == "-") answer = operand1 - operand2; else if (operation == "*") answer = operand1 * operand2; else if (operation == "/") answer = operand1 / operand2; numberBox.setText(Long.toString(answer)); operand1 = operand2 = answer = 0; operation = "+"; } private void doOperation(String newOperation) { if(newNumber) return; operand2 = Long.parseLong(numberBox.getText()); if (operation=="+") answer = operand1 + operand2; else if (operation=="-") answer = operand1 - operand2; else if (operation=="*") answer = operand1 * operand2; else if (operation=="/") answer = operand1 / operand2; numberBox.setText(Long.toString(answer)); operand1 = answer; operand2 = answer = 0; operation = newOperation; newNumber = true; } }