Source of ArrayBagDemo3.java


  1: /** A test of the methods remove and clear, as defined in the class ArrayBag.
  2:     Assumes the remaining methods are correct.
  3:     @author Frank M. Carrano, Timothy M. Henry
  4:     @version 5.0
  5: */
  6: public class ArrayBagDemo3
  7: {
  8:         public static void main(String[] args) 
  9:         {
 10:    // A bag that is not full
 11:                 BagInterface<String> aBag = new ArrayBag<>();
 12:       System.out.println("Testing an initially empty bag:");
 13:       
 14:       // Removing a string from an empty bag:
 15:       String[] testStrings1 = {"", "B"};
 16:       testRemove(aBag, testStrings1);

 18:       // Adding strings
 19:                 String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
 20:                 testAdd(aBag, contentsOfBag1);
 21:                 
 22:       // Removing strings
 23:                 String[] testStrings3 = {"", "B", "A", "C", "Z"};
 24:       testRemove(aBag, testStrings3);

 26:                 System.out.println("\nClearing the bag:");
 27:                 aBag.clear();
 28:       testIsEmpty(aBag, true);
 29:                 displayBag(aBag);

 31:         //----------------------------------------------------------------------

 33:                 // A bag that will be full
 34:                 aBag = new ArrayBag<String>(7);
 35:       System.out.println("\nA new empty bag:");

 37:       // Removing a string from an empty bag:
 38:       testRemove(aBag, testStrings1);

 40:       // Adding strings
 41:                 String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
 42:                 testAdd(aBag, contentsOfBag2);
 43:                 
 44:       // Removing strings
 45:                 System.out.println("\nRemoving strings:");
 46:       testRemove(aBag, testStrings3);

 48:                 System.out.println("\nClearing the bag:");
 49:                 aBag.clear();

 51:       testIsEmpty(aBag, true);
 52:                 displayBag(aBag);
 53:         } // end main
 54:         
 55:    // Tests the method add.
 56:    private static void testAdd(BagInterface<String> aBag, String[] content)
 57:    {
 58:       System.out.print("Adding the following strings to the bag: ");
 59:       for (int index = 0; index < content.length; index++)
 60:       {
 61:          if (aBag.add(content[index]))
 62:             System.out.print(content[index] + " ");
 63:          else
 64:             System.out.print("\nUnable to add " + content[index] +
 65:                              " to the bag.");
 66:       } // end for
 67:       System.out.println();
 68:       
 69:       displayBag(aBag);
 70:    } // end testAdd

 72:    // Tests the two remove methods.
 73:         private static void testRemove(BagInterface<String> aBag, String[] tests)
 74:         {
 75:       System.out.println("\nTesting the two remove methods:");
 76:       for (int index = 0; index < tests.length; index++)
 77:       {
 78:          String aString = tests[index];
 79:          if (aString.equals("") || (aString == null))
 80:          {
 81:             // test remove()
 82:             System.out.println("\nRemoving a string from the bag:");
 83:             String removedString = aBag.remove();
 84:             System.out.println("remove() returns " + removedString);
 85:          }
 86:          else
 87:          {
 88:             // Test remove(aString)
 89:             System.out.println("\nRemoving \"" + aString + "\" from the bag:");
 90:             boolean result = aBag.remove(aString);
 91:             System.out.println("remove(\"" + aString + "\") returns " + result);
 92:          } // end if
 93:          
 94:          displayBag(aBag);
 95:       } // end for
 96:         } // end testRemove

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

119:    // Tests the method toArray while displaying the bag.
120:         private static void displayBag(BagInterface<String> aBag)
121:         {
122:                 System.out.println("The bag contains " + aBag.getCurrentSize() +
123:                                    " string(s), as follows:");                
124:                 Object[] bagArray = aBag.toArray();
125:                 for (int index = 0; index < bagArray.length; index++)
126:                 {
127:                         System.out.print(bagArray[index] + " ");
128:                 } // end for
129:                 
130:                 System.out.println();
131:         } // end displayBag
132: } // end ArrayBagDemo

134: /*
135:  Testing an initially empty bag:
136:  
137:  Testing the two remove methods:
138:  
139:  Removing a string from the bag:
140:  remove() returns null
141:  The bag contains 0 string(s), as follows:
142:  
143:  
144:  Removing "B" from the bag:
145:  remove("B") returns false
146:  The bag contains 0 string(s), as follows:
147:  
148:  Adding to the bag: A A B A C A
149:  The bag contains 6 string(s), as follows:
150:  A A B A C A
151:  
152:  Testing the two remove methods:
153:  
154:  Removing a string from the bag:
155:  remove() returns A
156:  The bag contains 5 string(s), as follows:
157:  A A B A C
158:  
159:  Removing "B" from the bag:
160:  remove("B") returns true
161:  The bag contains 4 string(s), as follows:
162:  A A C A
163:  
164:  Removing "A" from the bag:
165:  remove("A") returns true
166:  The bag contains 3 string(s), as follows:
167:  A A C
168:  
169:  Removing "C" from the bag:
170:  remove("C") returns true
171:  The bag contains 2 string(s), as follows:
172:  A A
173:  
174:  Removing "Z" from the bag:
175:  remove("Z") returns false
176:  The bag contains 2 string(s), as follows:
177:  A A
178:  
179:  Clearing the bag:
180:  
181:  Testing the method isEmpty with an empty bag:
182:  isEmpty finds the bag empty: OK.
183:  The bag contains 0 string(s), as follows:
184:  
185:  
186:  A new empty bag:
187:  
188:  Testing the two remove methods:
189:  
190:  Removing a string from the bag:
191:  remove() returns null
192:  The bag contains 0 string(s), as follows:
193:  
194:  
195:  Removing "B" from the bag:
196:  remove("B") returns false
197:  The bag contains 0 string(s), as follows:
198:  
199:  Adding to the bag: A B A C B C D
200:  The bag contains 7 string(s), as follows:
201:  A B A C B C D
202:  
203:  Removing strings:
204:  
205:  Testing the two remove methods:
206:  
207:  Removing a string from the bag:
208:  remove() returns D
209:  The bag contains 6 string(s), as follows:
210:  A B A C B C
211:  
212:  Removing "B" from the bag:
213:  remove("B") returns true
214:  The bag contains 5 string(s), as follows:
215:  A C A C B
216:  
217:  Removing "A" from the bag:
218:  remove("A") returns true
219:  The bag contains 4 string(s), as follows:
220:  B C A C
221:  
222:  Removing "C" from the bag:
223:  remove("C") returns true
224:  The bag contains 3 string(s), as follows:
225:  B C A
226:  
227:  Removing "Z" from the bag:
228:  remove("Z") returns false
229:  The bag contains 3 string(s), as follows:
230:  B C A
231:  
232:  Clearing the bag:
233:  
234:  Testing the method isEmpty with an empty bag:
235:  isEmpty finds the bag empty: OK.
236:  The bag contains 0 string(s), as follows:
237: */