22 - Desk Calculators CS133: Developing Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Desk Calculators CS133: Developing Programming Principles Lecture 22 A Desk Calculator CS 133 Course Notes Lecture 22, Slide 2 1 2 Implementation User Interface CalculatorButton DecimalButton EmptyButton User Interface Application Logic ClearEntryButton EqualsButton ClearButton DigitButton OperatorButton CS 133 Course Notes Lecture 22, Slide 3 CS 133 Course Notes Lecture 22, Slide 4 3 4 CalculatorButton public abstract class CalculatorButton extends JButton implements ActionListener { private Calculator calculator; private JTextField display; // constructor and methods (next two slides) } CS 133 Course Notes Lecture 22, Slide 5 CS 133 Constructor public CalculatorButton ( String label, Calculator calculator, JTextField display) { super(label); this.calculator = calculator; this.display = display; addActionListener(this); } Course Notes Lecture 22, Slide 6 5 6 Methods public Calculator getCalculator() { return calculator; } public void actionPerformed(ActionEvent e) { pressed(); display.setText(calculator.toString()); } public abstract void pressed(); CS 133 Course Notes Lecture 22, Slide 7 Abstract Classes CalculatorButton is an example of an abstract class, which is used only as a base class to derive other classes. You can’t create an object of an abstract class. The pressed method is an example of an abstract method, which must be overridden in the derived class. CS 133 Course Notes Lecture 22, Slide 8 7 8 DigitButton public class DigitButton extends CalculatorButton { private int digit; public DigitButton( int digit,Calculator calculator,JTextField display ){ super(Integer.toString(digit), calculator, display); this.digit = digit; } public void pressed() { getCalculator().digit(digit); } } CS 133 Course Notes Lecture 22, Slide 9 CS 133 EqualsButton public class EqualsButton extends CalculatorButton { public EqualsButton( Calculator calculator, JTextField display ){ super("=", calculator, display); } public void pressed() { getCalculator().equals(); } } Course Notes Lecture 22, Slide 10 9 10 ClearButton public class ClearButton extends CalculatorButton { public ClearButton( Calculator calculator, JTextField display ){ super("AC", calculator, display); } public void pressed() { getCalculator().clear(); } } Components JTextField JPanel CS 133 Course Notes Lecture 22, Slide 11 CS 133 Course Notes Lecture 22, Slide 12 11 12 CalculatorPanel public class CalculatorPanel extends JPanel { private static final int DISPLAY = 16; // see next two slides } Constructor public CalculatorPanel() { Calculator calculator = new Calculator(); JTextField display = new JTextField(DISPLAY); display.setEditable(false); display.setBackground(Color.WHITE); JPanel buttons = calculatorButtons(calculator, display); setLayout(new BorderLayout()); add(display, BorderLayout.NORTH); add(buttons, BorderLayout.SOUTH); } CS 133 Course Notes Lecture 22, Slide 14 CS 133 Course Notes Lecture 22, Slide 13 13 14 calculatorButtons private JPanel calculatorButtons( Calculator calculator, JTextField display ){ JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(5,4)); buttons.add(new buttons.add(new buttons.add(new buttons.add(new buttons.add(new buttons.add(new // etc… } CS 133 Course Notes Lecture 22, Slide 15 CS 133 DeskCalculator public class DeskCalculator extends JFrame { public DeskCalculator() { Container content = getContentPane(); CalculatorPanel calculator = new CalculatorPanel(); content.add(calculator); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } } ClearButton(calculator,display)); ClearEntryButton(calculator,display)); EmptyButton(calculator,display)); EmptyButton(calculator,display)); DigitButton(7,calculator,display)); DigitButton(8,calculator,display)); Course Notes Lecture 22, Slide 16 15 16 Application Logic public class Calculator { public Calculator() {... } public public public public public public void void void void void void clear() {... } clearEntry() {... } digit(int digit) {... } decimal() {... } operator(Operator operator) {... } equals() {... } public new public new public new public new public new Operators static final Operator Operator("+"); static final Operator Operator("-"); static final Operator Operator("*"); static final Operator Operator("/"); static final Operator Operator(" "); ADD = SUBTRACT = MULTIPLY = DIVIDE = MOVE = public String toString() {... } ... } CS 133 Course Notes Lecture 22, Slide 17 CS 133 Course Notes Lecture 22, Slide 18 (argument to the operator method) 17 18 Testing the Class public static void main(String args) { Calculator c = new Calculator(); c.digit(1); System.out.println("1 = " + c); c.digit(0); System.out.println("10 = " + c); c.operator(ADD); System.out.println("10 = " + c); c.digit(2); System.out.println("2 = " + c); c.equals(); System.out.println("12 = " + c); } CS 133 Course Notes Lecture 22, Slide 19 Hidden Information In addition to what’s visible on the display, a calculator contains lots of hidden information: 1) a running total (accumulator) 2) an arithmetic operation (+, -, *, /) 3) display update status CS 133 Course Notes Lecture 22, Slide 20 19 20 Instance Variables // Values are entered into the display variable. // Results are computed and stored in the accumulator. private double display, accumulator; // The operation to apply when updating the accumulator. private Operator pendingOperation; // Do we clear the display variable before entering digits? private boolean clearEntryPending; // When a digit is pressed is it before the decimal point? private boolean beforeDecimal; // Position for next digit after the decimal point private double decimalPlace; CS 133 Course Notes Lecture 22, Slide 21 Constructor and Clearing public Calculator() { clear(); } public void clearEntry() { display = 0.0; clearEntryPending = false; beforeDecimal = true; decimalPlace = 0.1; } public void clear() { accumulator = 0.0; pendingOperation = MOVE; clearEntry(); } CS 133 Course Notes Lecture 22, Slide 22 21 22 Entering Digits public void digit(int digit) { assert digit >= 0 && digit <= 9; if (clearEntryPending) clearEntry(); if (beforeDecimal) { display = 10*display + digit; } else { display += decimalPlace*digit; decimalPlace *= 0.1; } } public void decimal() { if (clearEntryPending) clearEntry(); beforeDecimal = false; } CS 133 Course Notes Lecture 22, Slide 23 Equals public void equals() { if (pendingOperation == ADD) { accumulator += display; } else if (pendingOperation == SUBTRACT) { accumulator -= display; } else if (pendingOperation == MULTIPLY) { accumulator *= display; } else if (pendingOperation == DIVIDE) { accumulator /= display; } else { accumulator = display; } display = accumulator; clearEntryPending = true; pendingOperation = MOVE; } CS 133 Course Notes Lecture 22, Slide 24 23 24 Operators and Results public void operator(Operator operator) { equals(); pendingOperation = operator; } Improvements • Correct problems with display. • More functions (e.g., +/-, 1/x, sin, memory). • Make it look prettier. public String toString() { return Double.toString(display); } CS 133 Course Notes Lecture 22, Slide 25 CS 133 Course Notes Lecture 22, Slide 26 25 26 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online