100_lecture24

100_lecture24 - Introduction to Computation and Problem...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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. handles computation, the CPU and memory of the calculator, if you want to think of it that way. The application (
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

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 Right Arrow Icon
Ask a homework question - tutors are online