Source of ResizableArrayBagDemo.java


  1: /** A demonstration of the class ResizableArrayBag
  2:     @author Frank M. Carrano
  3:     @version 4.0
  4: */
  5: public class ResizableArrayBagDemo
  6: {
  7:         public static void main(String[] args) 
  8:         {
  9:                 // A bag whose initial capacity is small
 10:       BagInterface<String> aBag = new ResizableArrayBag<String>(3);
 11:       testIsEmpty(aBag, true);
 12:       
 13:                 System.out.println("Adding to the bag more strings than its initial capacity.");
 14:       String[] contentsOfBag = {"A", "D", "B", "A", "C", "A", "D"};
 15:                 testAdd(aBag, contentsOfBag);
 16:       testIsEmpty(aBag, false);
 17:       String[] testStrings2 = {"A", "B", "C", "D", "Z"};
 18:       testFrequency(aBag, testStrings2);
 19:       testContains(aBag, testStrings2);
 20:                 
 21:       // Removing strings
 22:                 String[] testStrings3 = {"", "B", "A", "C", "Z"};
 23:       testRemove(aBag, testStrings3);
 24:                 System.out.println("\nClearing the bag:");
 25:                 aBag.clear();
 26:       testIsEmpty(aBag, true);
 27:                 displayBag(aBag);
 28:         } // end main
 29:         
 30:    // Tests the method add.
 31:         private static void testAdd(BagInterface<String> aBag, String[] content)
 32:         {
 33:                 System.out.print("Adding to the bag: ");
 34:                 for (int index = 0; index < content.length; index++)
 35:                 {
 36:                         aBag.add(content[index]);
 37:          System.out.print(content[index] + " ");
 38:                 } // end for
 39:       System.out.println();
 40:       
 41:                 displayBag(aBag);
 42:         } // end testAdd
 43:    // Tests the two remove methods.
 44:         private static void testRemove(BagInterface<String> aBag, String[] tests)
 45:         {
 46:       for (int index = 0; index < tests.length; index++)
 47:       {
 48:          String aString = tests[index];
 49:          if (aString.equals("") || (aString == null))
 50:          {
 51:             // test remove()
 52:             System.out.println("\nRemoving a string from the bag:");
 53:             String removedString = aBag.remove();
 54:             System.out.println("remove() returns " + removedString);
 55:          }
 56:          else
 57:          {
 58:             // test remove(aString)
 59:             System.out.println("\nRemoving \"" + aString + "\" from the bag:");
 60:             boolean result = aBag.remove(aString);
 61:             System.out.println("remove(\"" + aString + "\") returns " + result);
 62:          } // end if
 63:          
 64:          displayBag(aBag);
 65:       } // end for
 66:         } // end testRemove
 67:    // Tests the method isEmpty.
 68:    // correctResult indicates what isEmpty should return.   
 69:         private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult)
 70:         {
 71:       System.out.print("Testing isEmpty with ");
 72:       if (correctResult)
 73:          System.out.println("an empty bag:");
 74:       else
 75:          System.out.println("a bag that is not empty:");
 76:       
 77:       System.out.print("isEmpty finds the bag ");
 78:       if (correctResult && aBag.isEmpty())
 79:                         System.out.println("empty: OK.");
 80:                 else if (correctResult)
 81:                         System.out.println("not empty, but it is empty: ERROR.");
 82:                 else if (!correctResult && aBag.isEmpty())
 83:                         System.out.println("empty, but it is not empty: ERROR.");
 84:                 else
 85:                         System.out.println("not empty: OK.");      
 86:                 System.out.println();
 87:         } // end testIsEmpty
 88:    // Tests the method getFrequencyOf.
 89:         private static void testFrequency(BagInterface<String> aBag, String[] tests)
 90:         {
 91:                  System.out.println("\nTesting the method getFrequencyOf:");
 92:       for (int index = 0; index < tests.length; index++)
 93:          System.out.println("In this bag, the count of " + tests[index] + 
 94:                             " is " + aBag.getFrequencyOf(tests[index]));
 95:    } // end testFrequency
 96:    
 97:    // Tests the method contains.
 98:         private static void testContains(BagInterface<String> aBag, String[] tests)
 99:         {
100:                  System.out.println("\nTesting the method contains:");
101:       for (int index = 0; index < tests.length; index++)
102:          System.out.println("Does this bag contain " + tests[index] + 
103:                             "? " + aBag.contains(tests[index]));
104:    } // end testContains
105:    // Tests the method toArray while displaying the bag.
106:         private static void displayBag(BagInterface<String> aBag)
107:         {
108:                 System.out.println("The bag contains " + aBag.getCurrentSize() +
109:                                    " string(s), as follows:");                
110:                 Object[] bagArray = aBag.toArray();
111:                 for (int index = 0; index < bagArray.length; index++)
112:                 {
113:                         System.out.print(bagArray[index] + " ");
114:                 } // end for
115:                 
116:                 System.out.println();
117:         } // end displayBag
118: } // end ResizableArrayBagDemo
119: /*
120:  Testing isEmpty with an empty bag:
121:  isEmpty finds the bag empty: OK.
122:  
123:  Adding to the bag more strings than its initial capacity.
124:  Adding to the bag: A D B A C A D
125:  The bag contains 7 string(s), as follows:
126:  A D B A C A D
127:  Testing isEmpty with a bag that is not empty:
128:  isEmpty finds the bag not empty: OK.
129:  
130:  
131:  Testing the method getFrequencyOf:
132:  In this bag, the count of A is 3
133:  In this bag, the count of B is 1
134:  In this bag, the count of C is 1
135:  In this bag, the count of D is 2
136:  In this bag, the count of Z is 0
137:  
138:  Testing the method contains:
139:  Does this bag contain A? true
140:  Does this bag contain B? true
141:  Does this bag contain C? true
142:  Does this bag contain D? true
143:  Does this bag contain Z? false
144:  
145:  Removing a string from the bag:
146:  remove() returns D
147:  The bag contains 6 string(s), as follows:
148:  A D B A C A
149:  
150:  Removing "B" from the bag:
151:  remove("B") returns true
152:  The bag contains 5 string(s), as follows:
153:  A D A A C
154:  
155:  Removing "A" from the bag:
156:  remove("A") returns true
157:  The bag contains 4 string(s), as follows:
158:  C D A A
159:  
160:  Removing "C" from the bag:
161:  remove("C") returns true
162:  The bag contains 3 string(s), as follows:
163:  A D A
164:  
165:  Removing "Z" from the bag:
166:  remove("Z") returns false
167:  The bag contains 3 string(s), as follows:
168:  A D A
169:  
170:  Clearing the bag:
171:  Testing isEmpty with an empty bag:
172:  isEmpty finds the bag empty: OK.
173:  
174:  The bag contains 0 string(s), as follows:
175: */