Chapter 9 Calculator
Chapter 9
Calculator
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-2
Chapter Objectives
• Provide a case study example from problem statement through implementation
• Demonstrate how a stack and a list can be used to solve a problem
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-3
A Calculator
• A calculator is a simple utility that is provided with most operating systems
• A calculator provides – a keypad of integer digits,
– the associated operations,
– other useful characters such as parentheses and brackets
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-4
A Calculator
• For this case study, we will create a graphical calculator that: – accepts an infix expression,
– converts that expression to postfix,
– evaluates the postfix expression,
– returns the result
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-5
A Calculator
• Our infix expressions may include– Integers
– Basic arithmetic operators • addition, subtraction, multiplication, division
– Parentheses
• All of the tokens in our expression must be separated by a space
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-6
A Calculator
• Our graphical user interface will allow the user to click on the numeric digits or simply press the numbers (and other characters) on the keyboard
• The user will click the equals button to start the evaluation of the given expression
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-7
A Calculator - Initial Design
• A calculator consists of four high-level components:– The driver
– The graphical user interface
– The infix to postfix converter
– The postfix evaluator
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-8
A Calculator - Initial Design
• Unlike the BlackJack case study, these high-level components are virtually unrelated, and we are not yet aware of any low-level components that will be needed
• Thus this problem lends itself to a top-down approach to design
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-9
A Calculator - the CalculatorDemo Class
• The CalculatorDemo class will serve as the driver for our system
• This class will simply instantiate the GUI and call its display method
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-10
A Calculator - the CalculatorGUI Class
• Other than the driver, the GUI is the highest-level component in this system
• The GUI must provide buttons to represent the digits, the operations, parentheses, and an equals button
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-11
FIGURE 9.1 The calculator user interface
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-12
A Calculator - the infixToPostfix Class
• The purpose of the infixToPostfix class to to provide a conversion method that accepts an infix expression as a string and returns the equivalent postfix expression
• Since we know that the postfix expression will then be evaluated,we will return the postfix expression as an unordered list rather than a string
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-13
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-14
A Calculator - the listPostfixEvaluator Class
• Like our example from Chapter 6, the listPostfixEvaluator class will take the postfix expression and use a stack to evaluate it
• The difference from our earlier example is that the expression has already been broken into tokens by the infixToPostfix class
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-15
A Calculator - the listPostfixEvaluator Class
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-16
FIGURE 9.2 UML description of the initial design of a calculator