Question: 1. Need to fix the error I am getting while printing Array List (Line 58 in Step3.java) to output file out2a.txt. 2. Construct a method

1. Need to fix the error I am getting while printing Array List (Line 58 in Step3.java) to output file out2a.txt.

2. Construct a method "infix2postfix" that takes infix string expression that is declared in the Step3.java class to convert to postfix and print its postfix form. Add it to output file by printing the output to 'out2a.txt' file.

3. Add a method "evaluate" that takes postfix form of expression from "infix2postfix" and evaluate the math expression and print its result to the output file too.

Step3.java:

import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer;

public class Step3 { public static void main(String[] args) { // Expression string String expression = "9.5 - 4 * 3 ^ 2 / 6 + 8 * 4";

// Create SymbolTable object SymbolTable st = new SymbolTable(expression); // Printing the required information in Console // Display my Name System.out.println(""); // Display expression System.out.println("Valid Expression: " + expression); // Display operator String System.out.println("Operator String: " + st.strOperator); System.out.println(); // Display symbol table System.out.println("Symbol Table: "); System.out.println(st); // Variable String System.out.println("Variable String: " + st.strVariable); // Display infix System.out.println("Infix of the expression: " + st.buildInfix());

// Printing the required information in Output File try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("C:/Users/patel/Desktop/JavaFiles/out2a.txt")))) { writer.write(""); ((BufferedWriter) writer).newLine(); writer.write(" Valid Expression: " + expression); ((BufferedWriter) writer).newLine(); writer.write("Operator String: " + st.strOperator); ((BufferedWriter) writer).newLine(); ((BufferedWriter) writer).newLine(); writer.write("Symbol Table: "); ((BufferedWriter) writer).newLine(); // GETTING ERRORS HERE int size = st.size(); for (int i = 0; i < size; i++) { writer.write(st.table); ((BufferedWriter) writer).newLine(); }

writer.write("Variable String: " + st.strVariable); ((BufferedWriter) writer).newLine(); writer.write("Infix of the expression: " + st.buildInfix()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

SymbolTable.java:

import java.util.ArrayList; import java.util.Scanner;

public class SymbolTable {

// Instance variables String strOperator; // Contains only the operators used in // expression String strVariable; // Contains only the variables (which take the // place of operands when symbol table is // complete ArrayList table = new ArrayList(); // symbol table

/** * Constructor */ public SymbolTable(String expression) { // Scan the expression for invalid characters String expr = scan(expression);

if (!expr.equals("")) { // If expression contains valid characters then // build the symbol table this.table = new ArrayList(); this.strOperator = new String(); this.strVariable = new String(); buildTable(expr); } }

/** * Accepts the expression string and returns the valid expression which is * without spaces. Returns empty string is the expression contains invalid * character * * @param expr * - Expression to scan */ private String scan(String expression) { String operators = "\\^|\\*|/|\\+|-";

// Replace multiple consecutive spaces by a single space expression = expression.trim().replaceAll("\\s+", " ");

// Scanner to scan the expression Scanner line = new Scanner(expression);

while (line.hasNext()) { if (!line.hasNextDouble() && !line.hasNext(operators)) { // Close scanner line.close(); return ""; } line.next(); }

// Close scanner line.close();

return expression.replaceAll("\\s+", ""); }

/** * This method constructs the symbol table from the valid expression */ private void buildTable(String expression) { String allowedOperators = "^*+/-"; String operand = ""; for (char ch : expression.toCharArray()) { if (!operand.equals("") && allowedOperators.contains(ch + "")) { int size = this.table.size(); char symbol = (char)('A' + size); Variable var = new Variable(symbol, Double.parseDouble(operand)); // Add variable to the table this.table.add(var); // Add symbol to strVariable this.strVariable += symbol; // Add operator to strOperator this.strOperator += ch; operand = ""; } else { operand += ch; } } // Check for the last operand if (!operand.equals("")) { int size = this.table.size(); char symbol = (char)('A' + size); Variable var = new Variable(symbol, Double.parseDouble(operand)); // Add variable to the table this.table.add(var); System.out.print("\t"); // Add symbol to strVariable this.strVariable += symbol; } }

/** * This method returns the infix from strVariable and strOperator. * * @return */ public String buildInfix() { if (this.table.size() != 0) { StringBuffer infix = new StringBuffer(); for (int i = 0; i < this.strVariable.length(); i++) { // Append variable infix.append(this.strVariable.charAt(i)); // Append operator if (i < this.strOperator.length()) infix.append(this.strOperator.charAt(i)); } return infix.toString(); } return "Cannot build infix. Please check the expression."; }

/** * Prints the contents of the table */ @Override public String toString() { if (this.table.size() != 0) { StringBuffer sb = new StringBuffer(); for (Variable variable : this.table) { sb.append(variable + " "); } return sb.toString(); } return "Symbol table not generated. Please check the expression."; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!