// Lab 1, CS 335J, Winter 2008 � Barbara Wahl // Alphabet Class // An alphabet is a non-empty, finite set of "symbols" and can be used to generate // a �language� (a set of strings). If S is an alphabet, S* denotes the language // containing all strings of length 0 or more made up from the symbols in alphabet S. // Since S* is infinite, it cannot be stored in its entirety on a computer, but in // our second lab we will add methods to generate and print all the strings in // S* up through a specified length n. import java.util.*; public class Alphabet { // ** DATA FIELDS ** protected StringBuffer alpha; // character symbols // ** CONSTRUCTORS ** public Alphabet(String inString) // fully-specified constructor // pre: inString is a non-empty string with no repeated characters // (characters should be listed in the desired lexicographic order) // post: creates an Alphabet object whose symbols are the characters in // inString { ??? // initialize data field "alpha" } public Alphabet() // no-arg constructor // post: creates alphabet {a, b} { ??? // use �this� to call the first constructor } // ** ACCESSOR METHODS ** public int getSize() // returns number of symbols in this alphabet ??? public String toString() // returns a String copy of alphabet symbols ??? public char charAt(int i) // post: returns i-th alphabet symbol (counting from position zero) // if i<0 (i too small), return first symbol // if i is too large, return last symbol ??? ??? ??? // ** OTHER METHODS ** public boolean indexArray(String inString, int[] array) // pre: array.length() >= inString.length() // post: if inString contains any characters not in the alphabet, // method returns "false"; otherwise it returns "true" and // for each 0 <= i < inString.length(), the ith char of inString // is converted to its position in the alphabet, and this integer // position is stored at array[i] { boolean flag = true; // set to false if find an illegal character int index; // for each character in inString ... for(int i=0; i<inString.length(); i++) { // use String class methods to find position of the symbol // in alpha index = ??? ; // set flag to false and break if it's not in alpha ??? // else, record its position in array ??? } return flag; } public void printAlphabet() // post: does pretty printing with braces and commas. // For example, if the alphabet symbols are abc, print "{a,b,c}" ??? // ** TEST METHOD ** public static void main(String[] args) { // Write code to test ALL the methods. Print info to // the console so it's easy to tell whether or not the // methods are working correctly. // To test: constructor #1, constructor #2, getSize, toString, // charAt, indexArray, printAlphabet. Alphabet[] A = new Alphabet[3]; A[0] = new Alphabet("xyz"); A[1] = new Alphabet(); A[2] = new Alphabet("#"); for(int i=0; i<3; i++) { ??? // testing code here for A[i] } } }