{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

100_lecture24

# 100_lecture24 - Introduction to Computation and Problem...

This preview shows pages 1–6. Sign up to view the full content.

1 Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 24: Class 24: Case Study: Case Study: Building a Simple Postfix Calculator Building a Simple Postfix Calculator 2 Goals To build a calculator to show how to divide functionality up into meaningful classes To introduce two useful software patterns : Model/View/Controller Finite State Machine 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3 expression notation called postfix rather than the infix notation that we are used to. Infix: Postfix: 13.5 1.5 Infix vs Postfix Notation Most early scientific calculators used an (13.5 – 1.5)/(3.4 + 0.6) - 3.4 0.6 + / 4 evaluation order in infix notation. or ? Infix vs Postfix, 2 Postfix notation gets rid of the parentheses that are necessary to determine precedence and Postfix is scanned left to right. Operators are evaluated as soon as they are encountered. If we remove the parentheses from our infix version, we get the ambiguous 13.5 – 1.5 / 3.4 + 0.6 Is that (13.5 – 1.5)/(3.4 + 0.6) 13.5 – (1.5 / 3.4) + 0.6 2
5 Postfix and Stacks is with a stack. Whenever the next item is a number, we push it on the stack. When the next item is an operator, the appropriate number of operands for the the operator is applied, and the result is pushed back on the stack. The best way to interpret postfix notation operator are popped off the stack, 6 Evaluating Postfix Infix: Postfix: Stack 13.5 13.5 1.5 1.5 13.5 - 12.0 3.4 3.4 12.0 0.6 0.6 3.4 12.0 + 4.0 12.0 / 3.0 (13.5 – 1.5)/(3.4 + 0.6) 13.5 1.5 - 3.4 0.6 + / Postfix Item 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
7 Software Patterns A software pattern is a design of a solution to a recurring software Patterns are independent of implementation language. They are more general and flexible than algorithms and data structures. problem. 8 Model/View/Controller (MVC) Model/View/Controller is a useful pattern for the design of interactive programs with a GUI MVC has strongly affected the Java event model. Event listeners are part of the controller. MVC has a long history going back to one of the earliest OO languages, Smalltalk. Event sources are part of the view. 4
9 MVC and our Calculator The view is the class ( Calculator ) that presents The controller is the class ( Calculator Controller processes events and updates the calculator The model is the class ( CalculatorModel ) that CalculatorApp ) main() . the user interface, the keypad and display. ) that interacts with the user, that without worrying about the details of the display.

This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

### Page1 / 19

100_lecture24 - Introduction to Computation and Problem...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online