Question
Part 1 is done: import java.util.List; import java.util.ArrayList; public class MyStack { ArrayList T = new ArrayList (); public MyStack() { } public void push(int
Part 1 is done:
import java.util.List;
import java.util.ArrayList;
public class MyStack
{
ArrayList
public MyStack()
{
}
public void push(int i)
{
T.add(0,i);
}
public void pop()
{ int sz=T.size();
T.remove(sz-1);
}
public int lookUp()
{
int sz=T.size();
int i=T.get(sz-1);
return i;
}
public boolean isEmpty()
{
T.isEmpty();
return true;
}
void display()
{
System.out.println(T);
}
Part 2:
Write the MathCalculator class to evaluate arithmetic expressions; it will make use of the generic stack class defined in part a.
Assume the following regarding the expression to evaluate:
all numbers are integers
the possible arithmetic operators are +, -, *, /, % and ^ where ^ is used for exponent
there may or may not be spaces between the operators, numbers and brackets
The minus sign always represents a subtraction; there is no unary minus sign in the expression. The result of a calculation may yield a negative number
there will not be any division by zero
The operators have the following priority of evaluation:
Highest priority ^
* / %
Lowest priority + -
It is important to know that:
operators of equal priority are evaluated from left to right
order of evaluation can be altered using brackets
the result of any operation is integer, including division.
The class will have 2 data members:
A String to store the arithmetic expression
An integer to store the result of the evaluation of the expression
The class should have the following public methods:
A zero-parameter constructor to set the arithmetic expression to the null String () and the result to zero
void evaluate (String) method to evaluate the given arithmetic expression (parameter) to obtain the result. Make sure to add a blank at the end of the given String before you start processing it.
String toString() returns the arithmetic expression followed by the equal sign and then the result. For example:
3 * 6 + ( 25 - 7 * 2 ) / 10 = 19
The evaluation of the arithmetic expression must be done according to the specifications below:
Evaluate as you process the string from left to right, taking into consideration the priority of operator and brackets, making use of an Integer stack and a Character stack.
Only go through the String with the arithmetic expression once.
Code private methods to efficiently produce the result. Methods (private or public) should not have more than 50 or 60 lines of code.
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