class HW2 { public static void main(String [] args) { String expressionString = "((7*(3+4))*(8+6))"; char [] cArray = convertToCharArray(expressionString); /* Write the code for evaluating the expression represented by the string expressionString here. The string has been converted into an array of characters cArray, which will be more convenient to work with. */ /* Printing out the cArray. Erase this code later.*/ for (int i = 0; i < cArray.length; i++) System.out.print(cArray[i]); System.out.println(" "); /* Below is an illustration of a method you can use to convert a character in the array cArray to the corresponding integer. You can remove this piece of code after you understand it. */ System.out.println(charToNum('8')); /* MyStack takes only objects. To push a char, you'll have to wrap it into a Character object. To retrieve the character from the Character object, use the instance method charValue. Erase this piece of code once understood. */ MyStack sta = new MyStack(); sta.push(new Character('a')); Character cc = (Character) sta.pop(); char c = cc.charValue(); System.out.println(c); /* Same principle for integers. int has to be wrapped into an Integer. Erase this piece of code once understood.*/ sta.push(new Integer(10)); Integer ii = (Integer) sta.pop(); int i = ii.intValue(); System.out.println(i); } static char [] convertToCharArray(String s) { /* Converts String s into character array */ char [] charArray = new char[s.length()]; for (int i = 0; i < s.length(); i++) charArray[i] = s.charAt(i); return charArray; } static int charToNum(char c) { /* Converts a character that corresponds to a digit to the corresponding integer. For example, '8' is converted to 8. If the input does not correspond to a digit, returns -1. */ if (Character.isDigit(c)) return Character.digit(c,10); return -1; } } /*new class*/ class ListEl { Object obj; ListEl next; public ListEl(Object o) { obj = o; next = null; } } /*new class*/ class MyStack { ListEl start; public MyStack() { start = null; } public void push (Object o) { ListEl newEl = new ListEl(o); newEl.next = start; start = newEl; } public Object pop () { if (start == null) {return null;} else { ListEl temp = start; start = temp.next; temp.next = null; return temp.obj; } } }