Question

In: Computer Science

Question 6 20 pts Compose the body of the following Java method: /* * Accepts an...

Question 6 20 pts

Compose the body of the following Java method:

/*
 * Accepts an array of Character consisting of "{[()]}" characters and 
 * returns true if all pairs of parentheses match.  Returns false if
 * parenthesis expression is malformed. Braces are allowed to occur 
 * inside brackets, and brackets are allowed to occur inside parentheses. 
 */

public boolean parenthesesMatch(Character [] input)
{

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/

public boolean parenthesesMatch(Character [] input) 
        {
                Stack<Character> stack = new Stack<Character>();
                for (int i = 0; i < input.length; i++) 
                {
                        char c = input[i];
                        if (c == '(' || c=='{' || c=='[' ) {
                                stack.push(c);
                        }
                        else if (c == ')' ||c== '}' || c==']') 
                        {
                                if (stack.size() == 0) 
                                {
                                        return false;
                                }
                                char p = stack.pop();
                                if (p == '(' && c==')' || p=='{' && c=='}' || p=='[' && c==']') 
                                {
                                        continue;
                                }
                                else
                                return false;
                        }
                }
        
                if (stack.size() != 0) {        
                        return false;
                }       
                return true;
        }

Related Solutions

Write one Java program and satisfy the following requirements: Write a method called cube that accepts...
Write one Java program and satisfy the following requirements: Write a method called cube that accepts one integer parameter and returns that value raised to the third power. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method) Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method) Overload findMax that accepts one...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
This is a java assignment Write the body of the fileAverage() method. Have it open the...
This is a java assignment Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage() header. Additionally, the file will have different contents during testing. public class Main { public...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write a program in Java which performs the sort operation. The main method accepts ten numbers...
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
Question 216 pts (TCO 6) Which of the following is used in processing ECG outputs to...
Question 216 pts (TCO 6) Which of the following is used in processing ECG outputs to determine heart rate? QR-interval RR-interval RT-interval SS-interval Flag this Question Question 226 pts (TCO 6) Permanent storage of measurements in a medical device utilizes which type of memory? DRAM SRAM Flash RAM RDRAM Flag this Question Question 236 pts (TCO 6) Microshock is a low-value current (microamps), which passes arm-to-arm through the body by skin contact with a voltage source. a low-value current (microamps),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT