Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In JAVA, write a brackets method called, check(). This method will be contained in a class called BracketChecker. This method will check that for each

In JAVA, write a brackets method called, check(). This method will be contained in a class called BracketChecker. This method will check that for each left delimiter, {, [, (, a right delimiter exists, ), ], }, and in the correct order.

For example, if { [ ( ( ) ) ] } is passed to the BracketChecker class, the check() method would return true since for every left delimiter there is a corresponding right delimiter and in the proper order. However, if { [ ( ( ) ] } is passed, an error should be flagged at index 5. A second ) was expected and instead a ] was encountered. the method also would return false, since an error occurred.

Because the delimiters are last-in-first-out, use a stack to insure that these delimiters are paired correctly. The user will enter the delimiters within the main() method and these delimiters will be passed to the BracketChecker class through the constructor.

The main() method is given below along with a StackX class to create the stack using an array. Do not make any modifications to the main() method or the StackX class. Use it as given.

public static void main(String[] args)

{

boolean flag = true;

String input;

Scanner keyboard = new Scanner(System.in);

while(flag)

{

System.out.print("Enter string containing delimiters: ");

input = keyboard.nextLine();

if( input.equals("") )

flag = false;

else

{

BracketChecker theChecker = new BracketChecker(input);

if (!theChecker.check())

System.out.println(" The delimiters are all correct. There were no errors ");

}

}

}

public class StackX { private int maxSize; private long[] stackArray; private int top;

public StackX(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; }

public void push(long j) { stackArray[++top] = j; }

public long pop() { return stackArray[top--]; }

public long peek() { return stackArray[top]; }

public boolean isEmpty() { return (top == -1); }

public boolean isFull() { return (top == maxSize-1); } }

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

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions