Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Problem - Symbol Balance Define a class called SymbolBalance.java SymbolBalance.java class will read through a Java file and check for simple syntactical errors. You

JAVA Problem - Symbol Balance

Define a class called SymbolBalance.java

SymbolBalance.java class will read through a Java file and check for simple syntactical errors. You should write two methods, as specified by the SymbolBalanceInterface which you must implement for full credit.

The first method, setFile, should take in a String representing the path to the file that should be checked.

The second method, checkFile, should read in the file character by character and check to make sure that all { }s, ( )'s, [ ]'s, " "s, and /* */s are properly balanced. Make sure to ignore characters within literal strings (" ") and comment blocks (/* */). Process the file by iterating through it one character at a time. During iteration, the symbol currently pointed to in the loop will be referred to as henceforth.

You do not need to handle single line comments (those that start with //), literal characters (things in single quotes), or the diamond operator(<>).

There are three types of errors that can be encountered:

  • The file ends with one or more opening symbols missing their corresponding closing symbols.
  • There is a closing symbol without an opening symbol.
  • There is a mismatch between closing and opening symbols (for example: { [ } ] ).

Once you encounter an error, return a BalanceError object containing error information. Each error type has its own class that descends from BalanceError and each has its own required parameters:

  • Symbol mismatch after popping stack: return MismatchError(int lineNumber, char currentSymbol, char symbolPopped)
  • Empty stack popped: EmptyStackError(int lineNumber)
  • Non-empty stack after parsing entire file: NonEmptyStackError(char topElement, int sizeOfStack)

If no error is found, return null.

Only push and pop the * character to the stack when handling multi-line comments. Do not push the /character or the string \*.

You must use MyStack.java in this problem.

Provided classes for errors:

//1

public interface SymbolBalanceInterface { public void setFile(String filename); public BalanceError checkFile(); // returns either MismatchError(int lineNumber, char currentSymbol, char symbolPopped) // EmptyStackError(int lineNumber), // NonEmptyStackError(char topElement, int sizeOfStack). // All three classes implement BalanceError

}

//2

public interface BalanceError { }

//3

public class EmptyStackError implements BalanceError {

public int line;

public EmptyStackError(int lineNumber) { line = lineNumber; }

public String toString() { return "EmptyStackError: {line:" + line + "}"; } }

//4

public class MismatchError implements BalanceError {

public int line; public char current; public char popped;

public MismatchError(int lineNumber, char currentSymbol, char symbolPopped) { line = lineNumber; current = currentSymbol; popped = symbolPopped; }

public String toString() { return "Mismatch Error: {line:" + line + ", current:" + current + ", popped:" + popped + "}"; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions