Question & Answer: need help with this code: have to complete the balanced code adding at the end of the…..

need help with this code: have to complete the balanced code adding at the end of the code: with providing a testing code for the code.

A stack is a data structure where data is accessed using the LIFO (last in first out) principle. In this problem, you will use a stack to check whether a string has balanced parentheses (, ) and brackets {, }, [, ], or not.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: need help with this code: have to complete the balanced code adding at the end of the…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

A string that has balanced parentheses and brackets will be said to be balanced. Any character that is not one of (, ), [, ], {, or } is not important when deciding if a string is balanced and can be ignored.

We will define balanced as follows. A string str is balanced

if str does not contain a parenthesis or bracket symbol, or

str consists of a balanced string surrounded by opening and closing parentheses or matching brackets. That is, str is (b), {b} or [b], where b is any balanced string, or

if str is the concatenation of any two balanced strings. That is, str is bc, where b and c are any balanced strings.

You will complete the provided Balanced class that has two static methods isBalanced(String) and numberOfBalancedStrings(String[]).

Your isBalanced method must use the java.util.Stack class (in a way that solves the problem) to receive any grades for this problem.

http://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

Examples

The following strings have balanced parentheses

(), ()()

cat, c(at), (hello)(kitty)

if( ((x-y) < 4) || (x > 12))

()(((s)))()()()()(x()((y))(x))()(ccccc(w))ssss()

The following strings do not have balances parentheses

), )(a), )a(

The following strings have balanced parentheses and brackets

a, [], {}, [()], []{}({[{}]}),

for(int i=0; i<12; i+=1){x[i]+=f(1);}

The following strings do not have balanced parentheses and brackets

(], {), [}, [}, (], ({)}h, [(]())

for(int i=0; i<12; i+=1){

Note: You will receive partial marks if you code only works for parentheses (and not brackets).

_________________________________

import java.util.Stack;
public class Balanced{
/**
Checks if a string is balanced or not.
@param s is a non-null string
@return true of the input <code>s</code> is balanced and false otherwise.
**/
public static boolean isBalanced(String s){
return false;
}
/**
Counts the number of balanced strings in the input array.
@param in is a non-null array of strings
@return the number of strings in the input <code>in</code> that are balanced.
**/
public static int numberOfBalancedStrings(String[] in){
return -1;
}
}

Expert Answer

 

Code:

//Include libraries

import java.util.Stack;

//Define a class

public class Balanced

{

//Define a stack

static Stack<Character> stack=new Stack<Character>();

//Define a method

public static boolean isBalanced(String s)

{

//Define a character array

char[] allChars=s.toCharArray();

//Loop

for(char c:allChars)

{

//If it is opening symbol

if(c=='(‘ || c=='{‘ || c=='[‘)

{

//Push

stack.push(c);

}

//Otherwise

else if(c==’)’ || c==’}’ || c==’]’)

{

//If stack not empty

if(!stack.isEmpty())

{

//If parenthesis match

if(stack.peek()=='(‘ && c==’)’)

{

//Pop stack

stack.pop();

}

//If braces match

else if(stack.peek()=='{‘ && c==’}’)

{

//Pop

stack.pop();

}

//otherwise

else if(stack.peek()=='[‘ && c==’]’)

{

//Pop

stack.pop();

}

//Otherwise

else

//Push

stack.push(c);

}

//Otherwise

else

//Return

return false;

}

}

//If stack is empty

if(stack.isEmpty())

//Return

return true;

//Clear

stack.clear();

//Return

return false;

}

//Define a method

public static int numberOfBalancedStrings(String[] in)

{

//Declare variable

int count=0;

//Loop

for(String s:in)

{

//If it is balanced

if(isBalanced(s))

{

//Increment count

++count;

}

}

//Return

return count;

}

//Define main

public static void main(String[] args)

{

//Call method

System.out.println(isBalanced(“()(((s)))()()()()(x()((y))(x))()(ccccc(w))ssss()”));

//Call method

System.out.println(isBalanced(“(], {), [}, [}, (], ({)}h, [(]())”));

//Define string array

String[] a = new String[3];

//Assign value

a[0] = “()”;

//Assign value

a[1] = “())”;

//Assign value

a[2] = “{{}”;

//Call method

int y = numberOfBalancedStrings(a);

//Display result

System.out.println(“Count of balanced string is “+y);

}

}

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.