Question: Part 2 Create and use a stack ADT (Abstract Data Type) to be used with a simple calculator. Our calculator will be capable of evaluating
Part 2
Create and use a stack ADT (Abstract Data Type) to be used with a simple calculator.
Our calculator will be capable of evaluating the following simple expression:
2.3 * 1.2 + 14.0
The operators for our simple calculator are as follows:
Addition +
Subtraction -
Multiplication *
Division /
The assignment involves three classes: But just focus on the calculator class since I have gotten someone else to answer the stack class. The driver class is complete and should not be modified. The other two classes are provided in incomplete format so they should be modified. You must fill in the details to make the class complete and for the driver to process correctly.
For the Calculator class, you will need to complete:
doOp()
repeatOps()
evalExp()
Only the bodies of these 3 methods need to be completed.
Upload your Calculator.java
The driver class is also listed down below for reference.
Calculator.java file
public class Calculator {
private StackvalStk; // the operand stack private Stack opStk; // the operator stack private int prec[] = new int[128]; // used for comparing precedent of operators // Constructor public Calculator() { valStk = new Stack (10); opStk = new Stack (10); prec['$'] = 0; // Used to signal end of input condition prec['+'] = 1; // + and - have lower precedence than * and / prec['-'] = 1; prec['*'] = 2; prec['/'] = 2; } /** * Take two operands from the valStack along with an operator from opStack and perform the * operation, pushing the result back on the valstack. * * @param obj * the object to be pushed */ void doOp() { // Begin: Insert your code here to complete the method // End: Of your code } /** * Repeat processing the operation while the operator on the top of the opStack * has a higher precedence than the current operator *
* @param refOp * the current operator */ void repeatOps(Character refOp) { // Begin: Insert your code here to complete the method // End: Of your code } /** * Evaluate a stream of tokens representing an arithmetic expression (with Doubles) *
* @param opstr * the string containing the expression, e.g., "3.0 * 2.0 + 1.0" *
* @return * true if the stack is empty, otherwise false * */ public Double evalExp(String opstr) { String[] tokens = opstr.split(" "); for (int i = 0; i < tokens.length; i++) { Double val; try { // Managing the exception val = Double.parseDouble(tokens[i]); } catch (NumberFormatException e) { val = null; } // Begin: Insert your code here to complete the method // End: Of your code } // Make sure all the operands have been processed by processing an operator // signaling the end // Begin: Insert your code here to complete the method // End: Of your code } }
Driver.java file
import java.io.BufferedReader;
import java.io.IOException; import java.io.InputStreamReader; public class Driver { public static void main(String[] args) throws IOException { // For example, entering: 2.3 * 1.2 + 14.0 - 100.5 // Displays: The result of 2.3 * 1.2 + 14.0 - 100.5 is -83.74000000000001 Calculator calc = new Calculator(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter an expression: "); String str = br.readLine(); System.out.print("The result of " + str + " is " + calc.evalExp(str)); } } (Please answer completely I broke it up into different parts so that I could get an answer.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
