//////////////////////////////////////////////////////////////////////////////// // Author: Julie Petrusa (991339) // // Program 1, Due date: January 23, 2001 // // This program defines a class CharSet that represents a set of characters // // drawn from the universe of the characters 'a' to 'z' lowercase. // // Known bugs: none // //////////////////////////////////////////////////////////////////////////////// import java.io.*; class CharSet { private int nElements; // will hold the number of elements in set A private int maxElements; // the maximum number of elements in the set private boolean[] booleanArray; // will hold the set //------------------------------------------------------------------------------ public CharSet() // constructor { nElements = 0; maxElements = 26; booleanArray = new boolean[maxElements]; // initialize: the set is empty for(int j=0; j Insert a character into set A"); System.out.println(" 2 --> Delete a character from set A"); System.out.println(" 3 --> Check if a character is a member of A"); System.out.println(" 4 --> Determine if A is a subset of B"); System.out.println(" 5 --> Display the union of A and B"); System.out.println(" 6 --> Display the intersection of A and B"); System.out.println(" 7 --> Display the difference of A and B"); System.out.println(" 8 --> Display the complement of set A"); System.out.println(" 9 --> Display the number of elements in set A"); System.out.println(" 10 --> Display set A"); System.out.println(" 11 --> Exit the program"); System.out.print("Answer: "); System.out.flush(); int answer = getInt(); // reads in the user's choice switch (answer) // do what the user wants { case 1: System.out.print("Enter the letter you want to insert: "); System.out.flush(); int letter1 = getChar(); // reads in the user's character A.insert(letter1); // inserts the character into A break; case 2: System.out.print("Enter the letter you want to delete: "); System.out.flush(); int letter2 = getChar(); // reads in the user's character A.delete(letter2); // deletes the character from A break; case 3: System.out.print("Enter the letter you want to find: "); System.out.flush(); int letter3 = getChar(); // reads in the user's character System.out.println(); // checks if it is a member of A if (A.member(letter3)) // and lets the user know System.out.println("It is a member"); else System.out.println("It is not a member"); break; case 4: System.out.println(); // determines if A is a subset of B if (A.subset(B)) // and lets the user know System.out.println("A is a subset of B"); else System.out.println("A is not a subset of B"); break; case 5: CharSet U = A.union(B); // displays A union B System.out.println(); System.out.println(U.toString()); break; case 6: CharSet I = A.intersection(B); System.out.println(); // displays A intersection B System.out.println(I.toString()); break; case 7: CharSet M = A.minus(B); // displays A minus B System.out.println(); System.out.println(M.toString()); break; case 8: CharSet C = A.comp(); // displays the complement of A System.out.println(); System.out.println(C.toString()); break; case 9: System.out.println(); // displays the cardinality of set A System.out.print("Cardinality: "); System.out.println(A.card()); break; case 10: System.out.println(); System.out.println(A.toString()); // displays the set A break; case 11: Value = false; break; default: System.out.println(); System.out.println("Invalid answer!"); break; } // end switch } while (Value != false); System.in.read(); } // end main() //------------------------------------------------------------------------------ public static String getString() throws IOException // reads in a string { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } //------------------------------------------------------------------------------ public static int getChar() throws IOException // reads in a character { String s = getString(); return s.charAt(0); } //------------------------------------------------------------------------------ public static int getInt() throws IOException // reads in an integer { String s = getString(); return Integer.parseInt(s); } //------------------------------------------------------------------------------ } // end class CharacterSets