Upcoming SlideShare
×

# Ch03

798 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
798
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
15
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch03

1. 1. Chapter 9 Calculator
2. 2. Chapter Objectives <ul><li>Provide a case study example from problem statement through implementation </li></ul><ul><li>Demonstrate how a stack and a list can be used to solve a problem </li></ul>
3. 3. A Calculator <ul><li>A calculator is a simple utility that is provided with most operating systems </li></ul><ul><li>A calculator provides </li></ul><ul><ul><li>a keypad of integer digits, </li></ul></ul><ul><ul><li>the associated operations, </li></ul></ul><ul><ul><li>other useful characters such as parentheses and brackets </li></ul></ul>
4. 4. A Calculator <ul><li>For this case study, we will create a graphical calculator that: </li></ul><ul><ul><li>accepts an infix expression, </li></ul></ul><ul><ul><li>converts that expression to postfix, </li></ul></ul><ul><ul><li>evaluates the postfix expression, </li></ul></ul><ul><ul><li>returns the result </li></ul></ul>
5. 5. A Calculator <ul><li>Our infix expressions may include </li></ul><ul><ul><li>Integers </li></ul></ul><ul><ul><li>Basic arithmetic operators </li></ul></ul><ul><ul><ul><li>addition, subtraction, multiplication, division </li></ul></ul></ul><ul><ul><li>Parentheses </li></ul></ul><ul><li>All of the tokens in our expression must be separated by a space </li></ul>
6. 6. A Calculator <ul><li>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 </li></ul><ul><li>The user will click the equals button to start the evaluation of the given expression </li></ul>
7. 7. A Calculator - Initial Design <ul><li>A calculator consists of four high-level components: </li></ul><ul><ul><li>The driver </li></ul></ul><ul><ul><li>The graphical user interface </li></ul></ul><ul><ul><li>The infix to postfix converter </li></ul></ul><ul><ul><li>The postfix evaluator </li></ul></ul>
8. 8. A Calculator - Initial Design <ul><li>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 </li></ul><ul><li>Thus this problem lends itself to a top-down approach to design </li></ul>
9. 9. A Calculator - the CalculatorDemo Class <ul><li>The CalculatorDemo class will serve as the driver for our system </li></ul><ul><li>This class will simply instantiate the GUI and call its display method </li></ul>
10. 10. A Calculator - the CalculatorGUI Class <ul><li>Other than the driver, the GUI is the highest-level component in this system </li></ul><ul><li>The GUI must provide buttons to represent the digits, the operations, parentheses, and an equals button </li></ul>
11. 11. FIGURE 9.1 The calculator user interface
12. 12. A Calculator - the infixToPostfix Class <ul><li>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 </li></ul><ul><li>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 </li></ul>
13. 14. A Calculator - the listPostfixEvaluator Class <ul><li>Like our example from Chapter 6, the listPostfixEvaluator class will take the postfix expression and use a stack to evaluate it </li></ul><ul><li>The difference from our earlier example is that the expression has already been broken into tokens by the infixToPostfix class </li></ul>
14. 15. A Calculator - the listPostfixEvaluator Class
15. 16. FIGURE 9.2 UML description of the initial design of a calculator