Question: JAVA This program will check if you balance your special symbols and these types are (), [], and {}. Explore the BalancedApp and Balanced class

JAVA

This program will check if you balance your special symbols and these types are (), [], and {}.

Explore the BalancedApp and Balanced class to better understand how the algorithm uses a stack to identify when an expression is well balanced.

Requirements: Using print statements, add comments to the code to produce the following output Enter an expression to be evaluated: (x(y()f) Push opening ( Ignoring 'x'. Push opening ( Ignoring 'y'. Push opening ( Trying to match ')', pop stack'(' matches ')' Ignoring 'f'. Trying to match ')', pop stack'(' matches ')' Premature end of expression. Not enough ending symbols. Evaluate another expression? (Y=Yes): Y Enter an expression to be evaluated: (a(a(b))) Push opening ( Ignoring 'a'. Push opening < Ignoring 'h'. Ignoring 'j'. Trying to match '>', pop stack'<' matches '>' Push opening ( Ignoring 'a'. Push opening ( Ignoring 'b'. Trying to match ')', pop stack'(' matches ')' Trying to match ')', pop stack'(' matches ')' Push opening < Ignoring 'h'. Trying to match '>', pop stack'<' matches '>' Trying to match ')', pop stack'(' matches ')' The symbols are balanced. Evaluate another expression? (Y=Yes): Y Enter an expression to be evaluated: {abc{def{hij}def)abc} Push opening { Ignoring 'a'. Ignoring 'b'. Ignoring 'c'. Push opening { Ignoring 'd'. Ignoring 'e'. Ignoring 'f'. Push opening { Ignoring 'h'. Ignoring 'i'. Ignoring 'j'. Trying to match '}', pop stack'{' matches '}' Ignoring 'd'. Ignoring 'e'. Ignoring 'f'. Trying to match ')', pop stack'{' does not match ')' Unbalanced symbols Evaluate another expression? (Y=Yes): N Make sure your print statements dont ignore closing characterssee example below: Enter an expression to be evaluated: (ab(cd)e) Push opening ( Ignoring 'a'. Ignoring 'b'. Push opening ( Ignoring 'c'. Ignoring 'd'. Trying to match ')', pop stack'(' matches ')' Ignoring ')'. Ignoring 'e'. Trying to match ')', pop stack'(' matches ')' Ignoring ')'. The symbols are balanced. Evaluate another expression? (Y=Yes):

Balanced java class

package ch03;

//----------------------------------------------------------------------

// Balanced.java by Dale/Joyce/Weems Chapter 3

//

// Checks for balanced expressions using standard rules.

//

// Matching pairs of open and close symbols are provided to the

// constructor through two string parameters.

//----------------------------------------------------------------------

import ch03.stacks.*;

public class Balanced

{

private String openSet;

private String closeSet;

public Balanced(String openSet, String closeSet)

// Preconditions: No character is contained more than once in the

// combined openSet and closeSet strings.

// The size of openSet = the size of closeSet.

{

this.openSet = openSet;

this.closeSet = closeSet;

}

public int test(String expression)

// Returns 0 if expression is balanced.

// Returns 1 if expression has unbalanced symbols.

// Returns 2 if expression came to end prematurely.

{

char currChar; // current expression character being studied

int currCharIndex; // index of current character

int lastCharIndex; // index of last character in the expression

int openIndex; // index of current character in openSet

int closeIndex; // index of current character in closeSet

boolean stillBalanced = true; // true as long as expression is balanced

BoundedStackInterface stack; // holds unmatched open symbols

stack = new ArrayStack(expression.length());

currCharIndex = 0;

lastCharIndex = expression.length() - 1;

while (stillBalanced && (currCharIndex <= lastCharIndex))

// while expression still balanced and not at end of expression

{

currChar = expression.charAt(currCharIndex);

openIndex = openSet.indexOf(currChar);

if(openIndex != -1) // if the current character is in the openSet

{

// Push the index onto the stack.

try {

stack.push(openIndex);

} catch (StackOverflowException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

else

{

closeIndex = closeSet.indexOf(currChar);

if(closeIndex != -1) // if the current character is in the closeSet

{

try // try to pop an index off the stack

{

openIndex = stack.top();

stack.pop();

if (openIndex != closeIndex) // if popped index doesn't match

{

stillBalanced = false; // then expression is not balanced

}

}

catch(StackUnderflowException e) // if stack was empty

{

stillBalanced = false; // then expression is not balanced

}

}

}

currCharIndex++; // set up processing of next character

}

if (!stillBalanced)

return 1; // unbalanced symbols

else

if (!stack.isEmpty())

return 2; // premature end of expression

else

return 0; // expression is balanced

}

}

Balanced App java class

package ch03; //---------------------------------------------------------------------- // BalancedApp.java by Dale/Joyce/Weems Chapter 3 // // Checks for balanced grouping symbols. // Input consists of a sequence of expressions, one per line. // Special symbol types are (), [], and {}. //----------------------------------------------------------------------

import java.util.Scanner;

public class BalancedApp { public static void main(String[] args) { Scanner conIn = new Scanner(System.in);

// Instantiate new Balanced class with grouping symbols. Balanced bal = new Balanced("([{<", ")]}>"); int result; // 0 = balanced, 1 = unbalanced, // 2 = premature end

String expression = null; // expression to be evaluated String more = null; // used to stop or continue processing

do { // Get next expression to be processed. System.out.println("Enter an expression to be evaluated: "); expression = conIn.nextLine(); // Obtain and output result of balanced testing. result = bal.test(expression); if (result == 1) { System.out.println("Unbalanced symbols "); } else if (result == 2){ System.out.println("Premature end of expression"); } else System.out.println("The symbols are balanced.");

// Determine if there is another expression to process. System.out.println(); System.out.print("Evaluate another expression? (Y=Yes): "); more = conIn.nextLine(); System.out.println(); } while (more.equalsIgnoreCase("y")); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!