Part 1 is done: import java.util.ArrayList; public class MyStack<T> { private Ar
ID: 3672739 • Letter: P
Question
Part 1 is done:
import java.util.ArrayList;
public class MyStack<T>
{
private ArrayList<T> stack;
public MyStack()
{
stack = new ArrayList<T>();
}
public MyStack (MyStack<T> oldStack)
{
int size = oldStack.size();
if(size == 0)
stack = new ArrayList<T>();
else{
while(oldStack.size() > 0){
stack.add(oldStack.size()-1, oldStack.pop());
}
int i=0;
while(i < size){
oldStack.push(stack.get(i));
}
}
}
public boolean isEmpty()
{
return size()==0;
}
public void push (T value)
{
stack.add(value);
}
public T pop()
{
if (size() != 0)
{
return stack.remove(size()-1);
}
else
{
System.out.println("Empty list, cannot list.");
return null;
}
}
public T lookUp()
{
if (size() != 0)
{
return stack.get(size()-1);
}
else
{
System.out.println("Empty list, cannot look up.");
return null;
}
}
public int size()
{
return stack.size();
}
}
Part 2: Write a class to evaluate arithmetic expressions, using the generic stack class from Part 1. The arithmetic operators are +, -, *, /, %, and ^ . The minus sign always represents a subtraction. The order of evaluation can be altered by using brackets. Assume that all values involved are integer values and that the result of any operation is also an integer. The highest priority is ^ and lowest priority is +.
Evaluate as you process the string, 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, from left to right. Code private methods to efficiently produce the result.
For example, it should calculate: 3+2*5-(2^3)+4
This is what I have so far:
import java.util.Scanner;
public class MathCalculator
{
String expression;
int result;
MyStack<Integer> intStack = new MyStack<Integer>();
MyStack<Character> charStack= new MyStack<Character>();
public MathCalculator()
{
expression = "";
result = 0;
}
//have to pop 2 numbers and operator first and then do calculations
public void evaluate (String expr)
{
String arithmetic = expr + " ";
int i = 0;
while(i < arithmetic.length())
{
if(Character.isDigit(expression.charAt(i)))
{
String temp = "";
while(Character.isDigit(arithmetic.charAt(i)))
{
temp += arithmetic.charAt(i);
i++;
}
intStack.push(Integer.parseInt(temp)); //converted to integer
}
(By the way, I'm a beginner Java programmer)
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mathcalculator;
/**
*
* @author HARE KRISHNA
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyStack<String> ops = new MyStack<String>();
MyStack<Double> vals = new MyStack<Double>();
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
if(s.equals("(")){
}
else if (s.equals("+")){
ops.push(s);
}
else if (s.equals("-")){
ops.push(s);
}
else if (s.equals("*")){
ops.push(s)
};
else if (s.equals("/")){
ops.push(s);
}
else if (s.equals("sqrt")){
ops.push(s);
}
else if (s.equals(")")) {
String op = ops.pop();
double v = vals.pop();
if (op.equals("+"))
v = vals.pop() + v;
else if (op.equals("-"))
v = vals.pop() - v;
else if (op.equals("*"))
v = vals.pop() * v;
else if (op.equals("/"))
v = vals.pop() / v;
else if (op.equals("sqrt"))
v = Math.sqrt(v);
vals.push(v);
}
else
vals.push(Double.parseDouble(s));
}
StdOut.println(vals.pop());
}
}
}