Source of ArrayBagDemo.java


  1: /** A demonstration of the class ArrayBag
  2:     @author Frank M. Carrano, Timothy M. Henry
  3:     @version 5.0
  4: */
  5: public class ArrayBagDemo
  6: {
  7:         public static void main(String[] args) 
  8:         {
  9:                 String[] contentsOfBag = {"A", "A", "B", "A", "C", "A"};

 11:       // Tests on an empty bag
 12:       BagInterface<String> aBag = new ArrayBag<>(contentsOfBag.length);
 13:       System.out.println("Testing an initially empty bag:");
 14:       testIsEmpty(aBag, true);
 15:       String[] testStrings1 = {"", "B"};
 16:       testFrequency(aBag, testStrings1);
 17:       testContains(aBag, testStrings1);
 18:       testRemove(aBag, testStrings1);

 20:       // Adding strings
 21:       System.out.println("Adding " + contentsOfBag.length +
 22:                          " strings to an initially empty bag with" +
 23:                          " the capacity to hold more than " +
 24:                          contentsOfBag.length + " strings:");
 25:                 testAdd(aBag, contentsOfBag);
 26:       
 27:       // Tests on a bag that is not empty
 28:       testIsEmpty(aBag, false);
 29:       String[] testStrings2 = {"A", "B", "C", "D", "Z"};
 30:       testFrequency(aBag, testStrings2);
 31:       testContains(aBag, testStrings2);
 32:                 
 33:       // Removing strings
 34:                 String[] testStrings3 = {"", "B", "A", "C", "Z"};
 35:       testRemove(aBag, testStrings3);

 37:                 System.out.println("\nClearing the bag:");
 38:                 aBag.clear();
 39:       testIsEmpty(aBag, true);
 40:                 displayBag(aBag);
 41:       
 42:       // Filling an initially empty bag to capacity
 43:       System.out.println("\nTesting an initially empty bag that " +
 44:                          " will be filled to capacity:");
 45:                 aBag = new ArrayBag<String>(7);
 46:                 String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
 47:                 testAdd(aBag, contentsOfBag2);
 48:       
 49:       System.out.println("Try to add another string to the full bag:");
 50:       if (aBag.add("another string"))
 51:          System.out.println("Added a string beyond the bag's capacity: ERROR!");
 52:       else
 53:          System.out.println("The method add cannot add another string: OK");
 54:         } // end main
 55:         
 56:    // Tests the method add.
 57:    private static void testAdd(BagInterface<String> aBag, String[] content)
 58:    {
 59:       System.out.print("Adding the following strings to the bag: ");
 60:       for (int index = 0; index < content.length; index++)
 61:       {
 62:          if (aBag.add(content[index]))
 63:             System.out.print(content[index] + " ");
 64:          else
 65:             System.out.print("\nUnable to add " + content[index] +
 66:                              " to the bag.");
 67:       } // end for
 68:       System.out.println();
 69:       
 70:       displayBag(aBag);
 71:    } // end testAdd
 72:    
 73:    // Tests the two remove methods.
 74:    private static void testRemove(BagInterface<String> aBag, String[] tests)
 75:    {
 76:       System.out.println("\nTesting the two remove methods:");
 77:       for (int index = 0; index < tests.length; index++)
 78:       {
 79:          String aString = tests[index];
 80:          if (aString.equals("") || (aString == null))
 81:          {
 82:             // test remove()
 83:             System.out.println("\nRemoving a string from the bag:");
 84:             String removedString = aBag.remove();
 85:             System.out.println("remove() returns " + removedString);
 86:          }
 87:          else
 88:          {
 89:             // Test remove(aString)
 90:             System.out.println("\nRemoving \"" + aString + "\" from the bag:");
 91:             boolean result = aBag.remove(aString);
 92:             System.out.println("remove(\"" + aString + "\") returns " + result);
 93:          } // end if
 94:          
 95:          displayBag(aBag);
 96:       } // end for
 97:    } // end testRemove

 99:    // Tests the method isEmpty.
100:    // correctResult indicates what isEmpty should return.   
101:         private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult)
102:         {
103:       System.out.print("Testing isEmpty with ");
104:       if (correctResult)
105:          System.out.println("an empty bag:");
106:       else
107:          System.out.println("a bag that is not empty:");
108:       
109:       System.out.print("isEmpty finds the bag ");
110:       if (correctResult && aBag.isEmpty())
111:                         System.out.println("empty: OK.");
112:                 else if (correctResult)
113:                         System.out.println("not empty, but it is empty: ERROR.");
114:                 else if (!correctResult && aBag.isEmpty())
115:                         System.out.println("empty, but it is not empty: ERROR.");
116:                 else
117:                         System.out.println("not empty: OK.");      
118:                 System.out.println();
119:         } // end testIsEmpty

121:    // Tests the method getFrequencyOf.
122:         private static void testFrequency(BagInterface<String> aBag, String[] tests)
123:         {
124:                  System.out.println("\nTesting the method getFrequencyOf:");
125:       for (int index = 0; index < tests.length; index++)
126:       {
127:          String aString = tests[index];
128:          if (!aString.equals("") && (aString != null))
129:          {
130:             System.out.println("In this bag, the count of " + tests[index] +
131:                                " is " + aBag.getFrequencyOf(tests[index]));
132:          } // end if
133:       } // end for
134:    } // end testFrequency
135:    
136:    // Tests the method contains.
137:         private static void testContains(BagInterface<String> aBag, String[] tests)
138:         {
139:                  System.out.println("\nTesting the method contains:");
140:       for (int index = 0; index < tests.length; index++)
141:       {
142:          String aString = tests[index];
143:          if (!aString.equals("") && (aString != null))
144:          {
145:             System.out.println("Does this bag contain " + tests[index] + 
146:                                "? " + aBag.contains(tests[index]));
147:          } // end if
148:       } // end for
149:    } // end testContains

151:    // Tests the method toArray while displaying the bag.
152:         private static void displayBag(BagInterface<String> aBag)
153:         {
154:                 System.out.println("The bag contains " + aBag.getCurrentSize() +
155:                                    " string(s), as follows:");                
156:                 Object[] bagArray = aBag.toArray();
157:                 for (int index = 0; index < bagArray.length; index++)
158:                 {
159:                         System.out.print(bagArray[index] + " ");
160:                 } // end for
161:                 
162:                 System.out.println();
163:         } // end displayBag
164: } // end ArrayBagDemo

166: /*
167:  Testing an initially empty bag:
168:  Testing isEmpty with an empty bag:
169:  isEmpty finds the bag empty: OK.
170:  
171:  
172:  Testing the method getFrequencyOf:
173:  In this bag, the count of B is 0
174:  
175:  Testing the method contains:
176:  Does this bag contain B? false
177:  
178:  Removing a string from the bag:
179:  remove() returns null
180:  The bag contains 0 string(s), as follows:
181:  
182:  
183:  Removing "B" from the bag:
184:  remove("B") returns false
185:  The bag contains 0 string(s), as follows:
186:  
187:  Adding 6 strings to an initially empty bag with the capacity to hold more than 6 strings:
188:  Adding A A B A C A
189:  The bag contains 6 string(s), as follows:
190:  A A B A C A
191:  Testing isEmpty with a bag that is not empty:
192:  isEmpty finds the bag not empty: OK.
193:  
194:  
195:  Testing the method getFrequencyOf:
196:  In this bag, the count of A is 4
197:  In this bag, the count of B is 1
198:  In this bag, the count of C is 1
199:  In this bag, the count of D is 0
200:  In this bag, the count of Z is 0
201:  
202:  Testing the method contains:
203:  Does this bag contain A? true
204:  Does this bag contain B? true
205:  Does this bag contain C? true
206:  Does this bag contain D? false
207:  Does this bag contain Z? false
208:  
209:  Removing a string from the bag:
210:  remove() returns A
211:  The bag contains 5 string(s), as follows:
212:  A A B A C
213:  
214:  Removing "B" from the bag:
215:  remove("B") returns true
216:  The bag contains 4 string(s), as follows:
217:  A A C A
218:  
219:  Removing "A" from the bag:
220:  remove("A") returns true
221:  The bag contains 3 string(s), as follows:
222:  A A C
223:  
224:  Removing "C" from the bag:
225:  remove("C") returns true
226:  The bag contains 2 string(s), as follows:
227:  A A
228:  
229:  Removing "Z" from the bag:
230:  remove("Z") returns false
231:  The bag contains 2 string(s), as follows:
232:  A A
233:  
234:  Clearing the bag:
235:  Testing isEmpty with an empty bag:
236:  isEmpty finds the bag empty: OK.
237:  
238:  The bag contains 0 string(s), as follows:
239:  
240:  
241:  Testing an initially empty bag that  will be filled to capacity:
242:  Adding A B A C B C D
243:  The bag contains 7 string(s), as follows:
244:  A B A C B C D
245:  Try to add another string to the full bag:
246:  The method add cannot add another string: OK
247:   */