Question: We will be creating and using a stack ADT (Abstract Data Type) to be used with a simple calculator. Our calculator will be capable of

We will be creating and using 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: Stack, Calculator, Driver. The driver class is complete and should not be modified. The other two classes are provided in skeleton format so they should be modified. You must fill in the details to make the class complete and for the driver to process correctly. A description of the algorithm can be found right here in these next two paragraphs if you would like to use them as a reference.

14 3 * 2 + 7 = (14 (3 * 2) ) + 7 Operator precedence * has precedence over +/ Associativity operators of the same precedence group evaluated from left to right Example: (x y) + z rather than x (y + z) Idea: push each operator on the stack, but first pop and perform higher and equal precedence operations.

Two stacks: opStk holds operators valStk holds values Use $ as special end of input token with lowest precedence Algorithm doOp() x ? valStk.pop(); y ? valStk.pop(); op ? opStk.pop(); valStk.push( y op x ) Algorithm repeatOps( refOp ): while ( valStk.size() > 1 ) while if (prec(refOp) ? if ( prec(opStk.top()) doOp() Algorithm EvalExp() Input: a stream of tokens representing an arithmetic expression (with numbers) Output: the value of the expression while theres another token z while if isNumber(z) then if then valStk.push(z) else repeatOps(z) opStk.push(z) repeatOps($) return valStk.top()

For the Stack class, you will need to complete:

top()

pop()

push()

isEmpty()

size()

For the Calculator class, you will need to complete:

doOp()

repeatOps()

evalExp()

Only the bodies of these 7 methods need to be completed. You do not need any additional code to make them work.

Upload your Stack.java and Calculator.java

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)); } }

Stack.java file

public class Stack {

 // Instance variables E stack[]; int top; int capacity; private final static int DEFAULT_SIZE = 30; // Constructors public Stack () { this(DEFAULT_SIZE); } public Stack (int capacity) { stack = (E[]) new Object[capacity]; this.top = -1; this.capacity = capacity; } // Other methods // Complete these methods for the assignment /** * Pushes an object on to the stack * 

* @param obj the object to be pushed */ public void push (E obj) { // Begin: Insert your code here to complete the method // End: Of your code } /** * Removes the top element on the stack and returns it to the caller *

* @return The top element of the stack, null if the stack is empty */ public E pop() { // Begin: Insert your code here to complete the method // End: Of your code } /** * Returns the top element on the stack to the caller *

* @return The top element of the stack, null if the stack is empty */ public E top() { // Begin: Insert your code here to complete the method // End: Of your code } /** * Returns true if the stack is empty *

* @return true if the stack is empty, otherwise false */ public boolean isEmpty() { // Begin: Insert your code here to complete the method // End: Of your code } /** * Returns the number of elements currently on the stack *

* @return number of elements */ public int size() { // Begin: Insert your code here to complete the method // End: Of your code } }

Please can somebody answer this problem the best that you can. This is my third time uploading this and still no answer.

Thanks in advance!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!