Balancer

// Balancer.java -- This class implements a recognizer for the language defined as follows:

//

//                  1. The language consists only of the characters a, b, and c.

//                  2. All valid words in the language are of the form:

//

//                      n m (n + m)

//                     a b c       , where n >= 0 and m >= 0.

//

//                     For example, aabbbccccc is in the language (n = 2, m = 3), but

//                     abc is not (n = 1, m = 1, but there is only one c). All a's must

//                     come before any b's, which must come before any c's.

//

// Name: 

// Section: 

//

// CSE 114, Spring 2006 (SUNY at Stony Brook)

import java.util.*;

public class Balancer

{

// This method takes a String as its argument and checks to see

// whether it conforms to the following rules:

//

// 1. The String contains only a's, b's, and c's

// 2. All a's precede all b's, which precede all c's

// 3. The number of c's is equal to the total number of a's and b's

//

// This method returns true if the argument conforms to these rules,

// and false if it does not.

//

// For example, abcc is valid, but abccc is not (there are 3 c's, but

// only 2 a's and b's). acb is not valid because b comes *after* c.

//

// Precondition: the argument contains only 'a', 'b', and 'c' characters.

//               You may assume that the input is all lowercase.

public static boolean isBalanced (String input)

{

// FILL IN THIS METHOD FOR QUESTION 3

return true;

}

// YOU MAY CHANGE THIS METHOD TO TEST YOUR SOLUTION

public static void main (String [] args)

{

String testString = "aabbccccc";

System.out.println("Test string: " + testString);

System.out.println();

System.out.print("This string is ");

if (isBalanced(testString) == false)

{

System.out.print("NOT ");

}

System.out.println("correctly formed.\n");

}

}

This page was last modified on 8/25/09