Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA JAVA JAVA JAVA JAVA JAVA Write a program that checks whether or not a strin

ID: 3671264 • Letter: J

Question

JAVA JAVA JAVA JAVA JAVA JAVA

Write a program that checks whether or not a string of characters that has enclosing symbols are well-formed. Enclosing meaning there is an open version of the symbol it and a closing symbol. Furthermore, well-formed means that if there is an opening symbol, then there must be a matching closing symbol also in the string.

The symbols to check are:

Parenthesis: ()

Double Quotes: “”

Curly Brackets / Braces: {}

Angle Brackets: <>

Write a stack, and use it to solve this problem

You may NOT use the built-in java stack

You may either implement the stack as a linked structure or an array. If you use an array assume the max size is 100;

Write another class that has a method which takes in a string and returns a Boolean that corresponds to whether or not the string is well formed. Use the following algorithm to determine if the string is well-formed.

Algorithm overview:

Examine each character one-by-one

If the character is the opening version of one of the enclosing symbols, then push that character onto the stack

If the character is the closing version of one of the enclosing symbols, then pop and check if it matches. If it does continue, but if not or if the stack is empty return false.

If there is a symbol that is not one of the enclosing symbols then ignore it

After each character has been process if the stack is empty return true. Otherwise return false

Finally write a class (it could be the same class as the class above) which uses a main method to test the following conditions:

() – should return true

<<<>>> - should return true

{(“tacos”)} – should return true

if(pass == true){return “yay!”;} – should return true

abcd – should return true

“””– should return false

<(“) – should return false

:-) – should return false

<3 – should return false

(<{“”}> - should return false

Explanation / Answer

public final class BalancedParanthesis { private static final Map brackets = new HashMap(); static { put('[', ']'); put('{', '}'); put('(', ')'); } private BalancedParanthesis() {}; /** * Returns true is parenthesis match open and close. * Understands [], {}, () as the brackets * It is clients responsibility to include only valid paranthesis as input. * A false could indicate that either parenthesis did not match or input including chars other than valid paranthesis * * @param str the input brackets * @return true if paranthesis match. */ public static boolean isBalanced(String str) { if (str.length() == 0) { throw new IllegalArgumentException("String length should be greater than 0"); } // odd number would always result in false if ((str.length() % 2) != 0) { return false; } final Stack stack = new Stack(); for (int i = 0; i