Question: Make a stack ADT (Abstract Data Type) to be used with a simple calculator. The calculator will be capable of evaluating the following simple expression:
Make a stack ADT (Abstract Data Type) to be used with a simple calculator.
The calculator will be capable of evaluating the following simple expression:
2.3 * 1.2 + 14.0
The operators for our simple calculator are as followed:
Addition +
Subtraction -
Multiplication *
Division /
The question involves completing one class and that is the calculator class aka (Calculator.java file): The driver class is complete and should not be modified. The calculator class needs to be completed because it is incomplete. 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().
The driver class is listed down below for reference.
What do I need to put in the code where it says "insert your code here to complete the method."?
Calculator.java file
public class Calculator { private Stack valStk; // 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)); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
