Question
We will be creating and using a stack ADT to be used with a simple calculator. Our calculator will be capable of evaluating the following
We will be creating and using a stack ADT 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. You must fill in the details to make the class complete and for the driver to process correctly. The description of the algorithm can be found in slides 18 and 19 of Chapter 6 on Stacks.
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.
Calculator.java class code
/*
* Assignment 3: Calculator Class
* Complete this class to implement the Calculator following the algorithms outlined * in slides 18 and 19 of Chapter 6. * * Name: * Date: * */ 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 class code
/* * Assignment 3: Driver Class * No not change the contents of this file * * Name: * Date: * */ 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 class code
/* * Assignment 3: Stack Class * Complete this class to implement the Stack * * Name: * Date: * */ 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 } }
Only include calculator.java and stack.java.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started