Due: Mon, Sep 2, by 11:59pm | screenshot of Calculator with buttons '+' and '=' as 3rd column and 1+2+3 shown on the screen |
Due: Thu, Sep 5, by 11:59pm | complete assignment |
Stack
data structure to keep track of intermediate results.
The final application that you turn in should work similar to s typical Calculator application. The calculator should work with decimal numbers and support the following operations: addition, subtraction, multiplication, division, exponentiation. In addition, the user should be allowed to use parentheses to group sub-expressions. The GUI should include the = button to indicate the end of expression and should have a clear button.
Calculator
.
For the CS Lab computers, here are instructions from CS112 on setting up Java 8 for JavaFX projects:
Java 8 for JavaFXYou may copy and modify the following starter code for your JavaFX application:
Calculator.javaPut each class in a separate file. Javadoc and formal testing is not required for this assignment.
1. create two stacks: * operand stack -- stores the numbers in the expression and intermediate results * operator stack -- stores operators that have not been evaluated 2. scan the input for individual tokens (either a number or an operator): 2.1. if current token is a number: store it on operand stack 2.2. otherwise (current token is an operator): as long as operator on top of the operator stack has higher or equal precedence to current operator: pop an operator and two operands evaluate the popped operator push the result back on the operand stack push current operator on top of operator stack [ Parentheses and = operators require extra consideration. ]
Calculator
.ButtonHandler
and the corresponding ActionEvent
s (there is no loop that "scans/runs" through the text/expression typed by the user).if-else
in Step 2.1/2.2 are implemented in ButtonHandler
and represent the decisions about which type of button was clicked.Operator ^ ^ ^ ____/ | \____________ / | \ / | \ AddOperator MulOperator ... RightParenOperator
private
in the final submission (could be declared protected
initially).
The operator classes will have a single constructor and two methods:
int getPrecedence()
-- returns the precedence level of this operatordouble evaluate(double a, double b)
-- returns the result of evaluating the operator on the given parameters
Note: Some of the operators (e.g. parentheses, equal sign) will not have appropriate implementation of the method evaluate
. For these operators, the method evaluate
should throw UnsupportedOperationException
.
print
statements in ButtonHandler
with code that adds the corresponding symbol to the screen TextField
each time a button is pressed.AddOperator
and make sure the calculator can evaluate simple expressions such as 1+2+, 1+2+3+
. The extra '+ at the end ensures that the actual expression to the left is evaluated completely and the result is on top of the stack. For now when =
is pressed, show the result, i.e. the value on the top of the stack, on the screen. After this works, it will be simple to add the remaining arithmetic operators and the proper '=' operator, so that the expressions look like 1+2=, 1+2+3=
.String
data member and "glue" onto it the corresponding symbol each time a number button is pressed. In this approach the number is built as a string and is put on the stack when we detect that non-number button is pressed.Calculator/ | +----- src/the .java files | +----- bin/