Assignment #11 will be the construction of a program that takes an input string,
ID: 3817342 • Letter: A
Question
Assignment #11 will be the construction of a program that takes an input string, and check if parentheses and braces in each string are matching or not.
The method checking() in the Matching class needs to check if parentheses and braces in the parameter string is matching or not using an object of the Stack class in java.util package.
Instruction:
Assignment11 class
This class displays a menu for the parentheses or braces checking. If a user enters "P", then it asks to enter a string. This class is given by the instructor.
Matching class
You need to complete the following method.
public static int checking(String inputString)
You need to write the checking method that takes an input string, and read every character from its left to right, then returns 0, 1, 2, 3, or 4 based on the following description.
It should create an empty Stack object at the beginning, and if a character is a left parenthesis '(' or a left brace '{', then push it (or its corresponding string such as "(" or "{") onto the stack.
If a character is a right parenthesis ')' and the top of the stack is '(', then pop it and continue to process the next character of the input string. But if the top of the stack is not '(' or the stack is empty, then that means that there was no matching left parenthesis, thus the method should return 1.
If a character is a right brace '}' and the top of the stack is '{', then pop it and continue to process the next character of the input string. But if the top of the stack is not '{' or the stack is empty, then that means that there was no matching left parenthesis, thus the method should return 3.
After reading all characters of the input string, if the stack is empty, then it means that everything is matching, the method should return 0.
After reading all characters of the input string, if the stack is not empty, and the top of the stack is a left parenthesis '(', then there was no matching right parenthesis, thus the method should return 2.
After reading all characters of the input string, if the stack is not empty, and the top of the stack is a left brace '{', then there was no matching right brace, thus the method should return 4.
The error messages are based on the first parenthesis or brace from left of the input string that does not have any matching.
Requirements:
You need to implement this method using an object of the Stack class) in java.util package.
import java.io.*;
public class Assignment11
{
public static void main (String[] args) throws IOException
{
char input1;
String inputInfo;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
// matches one of the case statements
switch (input1)
{
case 'P': //Enter String
System.out.print("Please enter a string. ");
inputInfo = stdin.readLine().trim();
int result = Matching.checking(inputInfo);
if (result == 0)
System.out.println("Everything is matching");
else if (result == 1)
System.out.println("right parenthesis does not have its matching left parenthesis");
else if (result == 2)
System.out.println("left parenthesis does not have its matching right parenthesis");
else if (result == 3)
System.out.println("right brace does not have its matching left brace");
else if (result == 4)
System.out.println("left brace does not have its matching right brace");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"P Enter String " +
"Q Quit " +
"? Display Help ");
}
}
Explanation / Answer
Mathing.java
import java.util.Stack;
public class Matching {
private static final char L_PAREN = '(';
private static final char R_PAREN = ')';
private static final char L_BRACE = '{';
private static final char R_BRACE = '}';
public static int checking(String inputString)
{
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == L_PAREN)
stack.push(L_PAREN);
else if (inputString.charAt(i) == L_BRACE)
stack.push(L_BRACE);
else if (inputString.charAt(i) == R_PAREN) {
if (stack.isEmpty())
return 1;
if (stack.pop() != L_PAREN)
return 1;
}
else if (inputString.charAt(i) == R_BRACE) {
if (stack.isEmpty())
return 3;
if (stack.pop() != L_BRACE)
return 3;
}
// ignore all other characters
}
return stack.isEmpty() ? 0 : (stack.pop() == L_PAREN ? 2 : 4);
}
}
Assignment11.java
import java.io.*;
public class Assignment11
{
public static void main (String[] args) throws IOException
{
char input1;
String inputInfo;
String line = new String();
printMenu();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = stdin.readLine();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{
// matches one of the case statements
switch (input1)
{
case 'P': //Enter String
System.out.print("Please enter a string. ");
inputInfo = stdin.readLine().trim();
int result = Matching.checking(inputInfo);
if (result == 0)
System.out.println("Everything is matching");
else if (result == 1)
System.out.println("right parenthesis does not have its matching left parenthesis");
else if (result == 2)
System.out.println("left parenthesis does not have its matching right parenthesis");
else if (result == 3)
System.out.println("right brace does not have its matching left brace");
else if (result == 4)
System.out.println("left brace does not have its matching right brace");
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"P Enter String " +
"Q Quit " +
"? Display Help ");
}
}