Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList (); public int getSize() { return list.size(); } public E peek() { return list.get(getSize() -

public class GenericStack { private java.util.ArrayList list = new java.util.ArrayList(); public int getSize() { return list.size(); } public E peek() { return list.get(getSize() - 1); } public void push(E o) { list.add(o); } public E pop() { E o = list.get(getSize() - 1); list.remove(getSize() - 1); return o; } public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return "stack: " + list.toString(); } }

You are teaching ITEC 2140s front part and you have so many homework to grade. You want to develop a program to check if students program has correct pairs of grouping symbols such as parentheses ( ), curly braces { }, and square brackets [ ]. Luckily you have a nice teaching assistant and s/he wrote a code as below, but you want to change it by using .split() method and regex (See Appendix H in your textbook) instead of using following code. You must read to identify the difference of using StringTokenizer and calling split() method from the following link.

http://enos.itcollege.ee/~jpoial/docs/docs9/api/java/util/StringTokenizer.html

Then, re-write the program by invoking split() method to tokenize the code. As you read the given code, you must read any of java program name as a command-line argument in the shell or terminal.

(e.g. javac MatchingGroupSysmbols.java to compile, java MatchingGroupSymbols.java Circle.java to run the program)

Case Study: Evaluating Expressions (p.803-807) will be greatly useful and a revised file of EvaluateExpression.java is posted on D2L.

public class MatchingGroupSymbols {

public static void main(String[] args) throws Exception {

GenericStack stack = new GenericStack>();

Scanner input = new Scanner(new File(args[0]));

try {

while (input.hasNext()) {

String s = input.nextLine();

StringTokenizer tokens = new StringTokenizer(s, "[](){}", true);

while (tokens.hasMoreTokens()) {

String token = tokens.nextToken().trim();

if (token.length() == 0)

continue;

else if (token.charAt(0) == '[') {

stack.push(']');

}

else if (token.charAt(0) == '{') {

stack.push('}');

}

else if (token.charAt(0) == '(') {

stack.push(')');

}

else if (token.charAt(0) == ']' ||

token.charAt(0) == '}' ||

token.charAt(0) == ')') {

char ch = ((Character)(stack.pop())).charValue();

if (ch != token.charAt(0)) {

System.out.println("Exit 1: Incorrect grouping pairs");

System.exit(1);

}

}

}

}

if (!stack.isEmpty()) {

System.out.println("Exit 2: Incorrect grouping pairs");

System.exit(2);

}

}

catch (Exception ex) {

System.out.println("Exit 3: Incorrect grouping pairs");

}

System.out.println("Correct grouping pairs");

}

}

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_2

Step: 3

blur-text-image_3

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

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

2. Do you find change a. invigorating? b. stressful? _______

Answered: 1 week ago