C Sc 227 Linked Stack and Balanced Symbols

1) Using Java's Stack class with the following four methods, Complete the main method started below so it scans the string alltext for unbalanced symbols. Use the provided algorithm to look for and report and unbalanced symbol errors. Feel free to write your own unique compiletime errors.

public boolean isEmpty(); // Return true if there are 0 elements

public void push(E element); // Put element on "top" of this Stack object.

public E peek(); // Return reference to top or throw new EmptyStackException()

public E pop(); // Remove and return element at top or throw EmptyStackException

1. Make an empty stack named s

2. For each character in the string representing an Java Program

if it's an opening symbol

push it onto the stack

else if it is a closing symbol & the stack is empty

report error with a println

increment number of errors

otherwise

pop the stack

if symbol is not a closer for pop's value

report error

increment number of errors

3. At end of file, if the stack is not empty, report error for every element still on the stack

4. Report the number of errors

import java.util.Stack;

public class BalancedSymbolChecker {

public static void main(String[] args) {

// Use allText as you java source code.

// Feel free to change it while testing

String allText = "public class A } \n"

+ " public static void main(String[args)] \n"

+ " System.out PRINTln( Hello ); \n"

+ " for( int j = 0; j < 6 j++ ) j++ \n"

+ " doub x = 0.0; \n"

+ " inTeger j = 0; \n"

+ " System.out.println(Goodbye]; \n"

+ " } \n"

+ "} \n"

+ "[{( \n";

System.out.println(allText);

// Apply the algorithm above to detect errors.

// Print the most accurate compile time error messages as possible

// Print the total number of unbalanced symbosl uinsg [], {}, and ()

2) If time permits, consider changes to linked structures during any of these methods from assignment 7

// Add data before all other elements in the double linked structure

public void addFirst(E data)

// Return and remove the first element from the doubly linked structure

public E removeFirst() throws NoSuchElementException

// Return and remove the last element from the doubly linked structure

public E removeLast() throws NoSuchElementException

// Return an object that has access to the linked structrure to allow elements

// to be visited from first to last using hasNext() and next()

private class ForwardItr implements ForwardIterator<E>