JAVA QUESTION - BalancedParentheses
Implement the class BalancedParentheses with a method isBalanced that receives a String and checks whether the brackets in it are balanced.
import java.util.Scanner;
public class BalancedParentheses {
public static void main(String[] args) {
checkParentheses("((a + b) * t/2 * (1 - t)");
checkParentheses("(a + b) * t)/(2 * (1 - t)");
checkParentheses("a + ((a + b) * t)/(2 * (1 - t))");
System.out.println("Enter an expression: ");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
checkParentheses(s);
}
public static void checkParentheses(String s) {
System.out.println(s + " is "
+ (isBalanced(s) ? "" : "not ")
+ "parentheses balanced");
}
public static boolean isBalanced(String s) {
// INSERT YOUR CODE HERE
}
}
//AS THE QUESTION DEMANDS ONLY BALANCING OF PARENTHESES SO OTHER
BRACKETS ARE NOT CONSIDERED//
/***********************CODE PROVIDED IN THE QUESTION
START***********************/
import java.util.Scanner;
import java.util.Stack; //FOR USING STACK IN THE PROGRAM
public class BalancedParentheses
{
public static void main(String[] args)
{
checkParentheses("((a + b) * t/2 * (1 - t)");
checkParentheses("(a + b) * t)/(2 * (1 - t)");
checkParentheses("a + ((a + b) * t)/(2 * (1 - t))");
System.out.println("Enter an expression: ");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
checkParentheses(s);
}
public static void checkParentheses(String s)
{
System.out.println(s + " is "+ (isBalanced(s) ? "" : "not ")+
"parentheses balanced");
}
/***********************CODE PROVIDED IN THE QUESTION
END***********************/
/**********************************ADDED
CODE***********************/
public static boolean isBalanced(String s)
{
Stack<Character> stack = new Stack<Character>();
char[] ch = s.toCharArray(); //CONVERT STRING TO CHARACTER
ARRAY
//SELECTIVELY ADDING THE EXPRESSION TO STACK
//ONLY OPEN PARENTHESES ARE ADDED TO THE STACK
for (char c : ch)
{ //push() AND pop() ARE USED FOR PUSH AND POP ON STACK
RESPECTIVELY
if(c == '(') //IF OPEN PARENTHESES IS ENCOUNTERED THEN PUSH IT TO
STACK
stack.push(c);
else if (c== ')') //IF CLOSING PARENTHESES IS ENCOUNTERD THEN POP
FROM STACK
{ //EACH CLOSING PARENTHESES POPS OUT CORESPONDING
OPEN PARENTHESES
if(stack.isEmpty()) //IF CLOSING PARENTHESES IS ENCOUNTED BEFORE AN
OPEN PARENTHESES
return false; //THAT IS STACK IS EMPTY AND POP() FUNCTION IS GOING
TO BE CALLED
stack.pop(); //THEN IT IS UNBALANCED USE OF PARENTHESES
}
}
return stack.isEmpty(); //isEmpty() RETURNS TRUE IF
STACK IS EMPTY AND VICE VERSA
}
}
JAVA QUESTION - BalancedParentheses Implement the class BalancedParentheses with a method isBalanced that receives a String...
In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); } public static String printBackwards(String one)...
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...
Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv { public static void outputMinutesAsHours(double origMinutes) { /* Your solution goes here */ } public static void main (String [] args) { Scanner scnr = new Scanner(System.in); double minutes; minutes = scnr.nextDouble(); outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0. System.out.println(""); } } 2....
CHALLENGE ACTIVITY 3.11.1: Using boolean. D Assign is Teenager with true if kidAge is 13 to 19 inclusive. Otherwise, assign is Teenager with false. 1 import java.util.Scanner; 3 public class TeenagerDetector 1 public static void main (String [] args) { Scanner scnr = new Scanner(System.in); boolean isTeenager; int kidAge; D}]oll kidage = scnr.nextInt(); /* Your solution goes here */ Go USB if (isTeenager) { System.out.println("Teen"); else { System.out.println("Not teen"); 20 Run Feedback? CHALLENGE ACTIVITY 3.11.2: Boolean in branching statements. Write...
JAVA: Write a program that prints a rectangle with a border of asterisks (*). Prompt the user for the width and height of the rectangle. For example: Enter the width and the height of our box. 3 5 *** * * * * * * *** import java.util.Scanner; public class DrawBox { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the width and the height of our box."); /* Type your code here. */...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width; public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...
Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...
In JAVA
ACTIVITY 3.10.1: Rock-paper-scissors. Write a switch statement that checks nextChoice. If o print "Rock". If 1 print "Paper". If 2 print "Scissors'. For any other value, print "Unknown'. End with newline. 2. 4 1 import java.util.Scanner; 3 public class Roshambo public static void main(String [] args) { 5 Scanner scnr - new Scanner(System.in); 6 int nextChoice; 7 8 nextChoice - scnr.nextInt(); 9 10 /" Your solution goes here */ 11 12 13} CHALLENGE ACTIVITY 3.10.2: Switch statement to...
I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact { public static void main(String[] args) { Random Rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println("welcome to the contact application"); System.out.println(); int die1; int die2; int Total; String choice = "y";...