Question: public class ParenthesesBracketsMatch { public static boolean AreParenthesesBracketsNested(String str) { // Create a stack java.util.Stack stack = new java.util.Stack (); for (int i=0; i
public class ParenthesesBracketsMatch { public static boolean AreParenthesesBracketsNested(String str) { // Create a stack java.util.Stack public static void main(String[] args) { //DO NOT CHANGE THE MAIN METHOD String str1 = "{[()]()}"; boolean matched1 = AreParenthesesBracketsNested(str1); System.out.println(str1 + " is nested properly: " + matched1); String str1b = "{a+[b*(10+10)]*(c-d)}"; boolean matched1b = AreParenthesesBracketsNested(str1b); System.out.println(str1b + " is nested properly: " + matched1b); String str2 = "([()]{}"; boolean matched2 = AreParenthesesBracketsNested(str2); System.out.println(str2 + " is nested properly: " + matched2); String str2b = "(a+[b*(e-f)]-{a+b}"; boolean matched2b = AreParenthesesBracketsNested(str2b); System.out.println(str2b + " is nested properly: " + matched2b); String str3 = "[{}]()]()"; boolean matched3 = AreParenthesesBracketsNested(str3); System.out.println(str3 + " is nested properly: " + matched3); String str4 = "[{)]()"; boolean matched4 = AreParenthesesBracketsNested(str3); System.out.println(str4 + " is nested properly: " + matched4); String str5 = "[{})()"; boolean matched5 = AreParenthesesBracketsNested(str5); System.out.println(str5 + " is nested properly: " + matched5); java.util.Scanner input = new java.util.Scanner(System.in); System.out.println("Enter a string of (, ), [, ], {, and }:"); String str = input.next(); boolean matched = AreParenthesesBracketsNested(str); System.out.println(str + " is nested properly: " + matched); } } import java.util.Stack; import java.util.Scanner; public class EvaluateExpression2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an arithmetic expression: "); String expr = input.nextLine(); System.out.println(expr + " = " + evaluateExpression(expr)); } //TODOs: Adjust the two methods in the example to adapt to possible double operands public static String insertBlanks(String s) { String result = ""; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(' || s.charAt(i) == ')' || s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' || s.charAt(i) == '/') result += " " + s.charAt(i) + " "; else result += s.charAt(i); } return result; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
