Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIS211-401: Data Structures Exam #2: Part II-Step 3 Part ll-Step 3: Test if you

ID: 3600181 • Letter: C

Question

CIS211-401: Data Structures Exam #2: Part II-Step 3 Part ll-Step 3: Test if you have done your labs properly a table and RPN. (80 points) and understand how to build symbol 1. Complete the main program with the following statements r elated to Stepa String expression "5+6 2-9/3/2 String infix "A+B C-D/E/F And it's you job to find the format specifier for printing the computational result in 2 decimal places 2. By now you should have constructed the following classes: 1. Variable: A cvariable, values pair 2. SymbolTable: for this exam, you will need to use the following methods a. Constructor; b. buildTable: //that takes a string and build the symbol table c. find; //that takes variable and returns its value d. toString)://print the symbol table, see output sample 3. Modify a little bit of ConverToPostfix.java and rename it to Step3 java. Instead of using author's Stack interface, you need to your MyStack class. Besides, don't forget that you MyStack takes STACK_SIZE at constructor. The main program should have provided you with enough information for completing this step 4. What you need to turn in for me to grade: This page with your name written. The following java program: a. Step3.java b. Variable.java c. SymbolTable.java 1) 2) Finally, make sure you have uploaded all ava files and two input text files to your soogle drive under exam2 folder. And then have all these files zipped and submit it to BlackBoard When your program successfully runs, it should produce the following output (for 3 steps):

Explanation / Answer

import java.io.IOException;

public class ConvertToPostfix {
private stack st;
private String IString;
private String OString = "";
public ConvertToPostfix(String in) {
IString = in;
int St_Size = IString.length();
st = new stack(St_Size); //stack take its size as parameter of its constructor
}
public String doConvert() {
for (int j = 0; j < IString.length(); j++) {
char ch = IString.charAt(j);
switch (ch) {
case '+':
case '-':
gotOperator(ch, 1);
break;
case '*':
case '/':
gotOperator(ch, 2);
break;
case '(':
st.push(ch);
break;
case ')':
gotParenthesis(ch);
break;
default:
OString = OString + ch;
break;
}
}
while (!st.isEmpty()) {
OString = OString + st.pop();
}
return OString;
}
public void gotOperator(char opThis, int prec1) {
while (!st.isEmpty()) {
char opTop = st.pop();
if (opTop == '(') {
st.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
st.push(opTop);
break;
}
else OString = OString + opTop;
}
}
st.push(opThis);
}
public void gotParenthesis(char ch) {
while (!st.isEmpty()) {
char chx = st.pop();
if (chx == '(')
break;
else OString = OString + chx;
}
}
public static void main(String[] args) throws IOException {
String IString = "134+9*38*5/7+3/6";
System.out.println("Infix is "+IString);
String OString;
ConvertToPostfix theTrans = new ConvertToPostfix(IString);
OString = theTrans.doConvert();
System.out.println("Converted Postfix is " + OString );
}
class stack {
private int size_max;
private char[] stack_arr;
private int top;

public stack(int max) { //stack constructor take its size as parameter
size_max = max;
stack_arr = new char[size_max];
top = -1;
}
public void push(char j) {
stack_arr[++top] = j;
}
public char pop() {
return stack_arr[top--];
}
public char peek() {
return stack_arr[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}