Plan for Today General Questions/Confusions Assignment #2 – Overview & Questions –The Calculator Class –The Converter Class –Sample Input/Output Assignment #2 – The Command-line & Keyboard Input 2
3 Assignment #2: Calculator Introduction Your project is to design a program to implement a calculator. The calculator will take an infix expression convert it to a postfix expression and then evaluate it. Review: infix and postfix notation
4 Assignment #2: Calculator You should implement the calculator in two parts: A Converter class that will convert the input string to postfix. A Calculator class that will evaluate the postfix expression.
5 Assignment #2: Calculator The text contains explanations of the algorithms you will need. See pages 81 - 84 for the algorithm you should use for the Calculator class See pages 84 - 86 for the Converter algorithm (Another explanation of the two algorithms can be found here).here You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.PostCalc
6 Assignment #2: Calculator You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment. PostCalc Both the Converter and Calculator classes, should use the StackLI class for the Stack.StackLI Are the methods in StackLI & PostCalc.java clear to everyone? StackLI
7 Assignment #2: Calculator The Calculator class Basically this class will be the same as the PostCalc class shown in lecture. The main differences will be: –The input will be an infix expression instead of a postfix expression. –It will be entered by the user instead of hard- coded by you. –The input may have operands which consist of multiple digits -- not just single digits like PostCalc
8 Assignment #2: Calculator –Parentheses are legal in the infix expression (Note: The parentheses are needed in the infix expression. After your Converter class' algorithm converts the expression to postfix, it will no longer have parentheses because they are not necessary in postfix expressions.) –The Calculator class will instantiate an object of the Converter class in order to have the infix expression converted to a postfix expression. –You may have to modify the other methods in the PostCalc class (and/or add additional ones).
9 Assignment #2: Calculator The Converter class The Converter class will use a Stack in order to implement the algorithm in the book. When the Converter class is instantiated, a String is passed representing the infix expression entered. This should be saved as an instance variable. The method that does all the work in the Converter class should be called toPostFix(). It will convert the infix expression to a postfix expression. (It should return a Queue (use the queue from the book) of Strings representing the postfix expression to the Calculator class. The details and helper methods used in this class are left up to you.
10 Assignment #2: Calculator The Converter class: Hint You will probably want to implement a method that compares two operators for precedence: /** * @return 1 if opA has higher precedence * -1 if opB has higher precedence * 0 if they have equal precedence */ int comparePrecedence(char opA, char opB);
11 Assignment #2: Calculator Sample inputs –3+4*5/6 –(300+23)*(43-21)/(84+7) –(4+8)*(6-5)/((3-2)*(2+2)) Your output should show the converted postfix string and the result of the calculation.
12 Assignment #2: Calculator For instance, here is a sample run of the program (user input in italic, program output in courier ): The input infix expression: $ (4+8)*(6-5)/((3-2)*(2+2)) INFIX: (4+8)*(6-5)/((3-2)*(2+2)) Converted to postfix: POSTFIX: 4 8 + 6 5 - * 3 2 - 2 2 + * / And the answer: RESULT: 3.0
13 Assignment #2: Calculator For a given input at prompt $ $ (4+8)*(6-5)/((3-2)*(2+2)) INFIX: (4+8)*(6-5)/((3-2)*(2+2)) POSTFIX: 4 8 + 6 5 - * 3 2 - 2 2 + * / RESULT: 3.0 Questions?