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
  4:     @version 4.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);
 17:       // Adding strings
 18:                 String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
 19:                 testAdd(aBag, contentsOfBag1);
 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:         //----------------------------------------------------------------------
 29:                 // A bag that will be full
 30:                 aBag = new ArrayBag<String>(7);
 31:       System.out.println("\nA new empty bag:");
 32:       // Removing a string from an empty bag:
 33:       testRemove(aBag, testStrings1);
 34:       // Adding strings
 35:                 String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
 36:                 testAdd(aBag, contentsOfBag2);
 37:                 
 38:       // Removing strings
 39:                 System.out.println("\nRemoving strings:");
 40:       testRemove(aBag, testStrings3);
 41:                 System.out.println("\nClearing the bag:");
 42:                 aBag.clear();
 43:       testIsEmpty(aBag, true);
 44:                 displayBag(aBag);
 45:         } // end main
 46:         
 47:    // Tests the method add.
 48:         private static void testAdd(BagInterface<String> aBag, String[] content)
 49:         {
 50:                 System.out.print("Adding to the bag: ");
 51:                 for (int index = 0; index < content.length; index++)
 52:                 {
 53:                         aBag.add(content[index]);
 54:          System.out.print(content[index] + " ");
 55:                 } // end for
 56:       System.out.println();
 57:       
 58:                 displayBag(aBag);
 59:         } // end testAdd
 60:    // Tests the two remove methods.
 61:         private static void testRemove(BagInterface<String> aBag, String[] tests)
 62:         {
 63:       System.out.println("\nTesting the two remove methods:");
 64:       for (int index = 0; index < tests.length; index++)
 65:       {
 66:          String aString = tests[index];
 67:          if (aString.equals("") || (aString == null))
 68:          {
 69:             // test remove()
 70:             System.out.println("\nRemoving a string from the bag:");
 71:             String removedString = aBag.remove();
 72:             System.out.println("remove() returns " + removedString);
 73:          }
 74:          else
 75:          {
 76:             // Test remove(aString)
 77:             System.out.println("\nRemoving \"" + aString + "\" from the bag:");
 78:             boolean result = aBag.remove(aString);
 79:             System.out.println("remove(\"" + aString + "\") returns " + result);
 80:          } // end if
 81:          
 82:          displayBag(aBag);
 83:       } // end for
 84:         } // end testRemove
 85:    // Tests the method isEmpty.
 86:    // correctResult indicates what isEmpty should return.   
 87:         private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult)
 88:         {
 89:       System.out.print("\nTesting the method isEmpty with ");
 90:       if (correctResult)
 91:          System.out.println("an empty bag:");
 92:       else
 93:          System.out.println("a bag that is not empty:");
 94:       
 95:       System.out.print("isEmpty finds the bag ");
 96:       if (correctResult && aBag.isEmpty())
 97:                         System.out.println("empty: OK.");
 98:                 else if (correctResult)
 99:                         System.out.println("not empty, but it is empty: ERROR.");
100:                 else if (!correctResult && aBag.isEmpty())
101:                         System.out.println("empty, but it is not empty: ERROR.");
102:                 else
103:                         System.out.println("not empty: OK.");      
104:         } // end testIsEmpty
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 ArrayBagDemo
119: /*
120:  
121:  Testing an initially empty bag:
122:  Testing the two remove methods:
123:  
124:  Removing a string from the bag:
125:  remove() returns null
126:  The bag contains 0 string(s), as follows:
127:  
128:  
129:  Removing "B" from the bag:
130:  remove("B") returns false
131:  The bag contains 0 string(s), as follows:
132:  
133:  Adding to the bag: A A B A C A
134:  The bag contains 6 string(s), as follows:
135:  A A B A C A
136:  
137:  Testing the two remove methods:
138:  
139:  Removing a string from the bag:
140:  remove() returns A
141:  The bag contains 5 string(s), as follows:
142:  A A B A C
143:  
144:  Removing "B" from the bag:
145:  remove("B") returns true
146:  The bag contains 4 string(s), as follows:
147:  A A C A
148:  
149:  Removing "A" from the bag:
150:  remove("A") returns true
151:  The bag contains 3 string(s), as follows:
152:  A A C
153:  
154:  Removing "C" from the bag:
155:  remove("C") returns true
156:  The bag contains 2 string(s), as follows:
157:  A A
158:  
159:  Removing "Z" from the bag:
160:  remove("Z") returns false
161:  The bag contains 2 string(s), as follows:
162:  A A
163:  
164:  Clearing the bag:
165:  
166:  Testing the method isEmpty with an empty bag:
167:  isEmpty finds the bag empty: OK.
168:  The bag contains 0 string(s), as follows:
169:  
170:  
171:  A new empty bag:
172:  
173:  Testing the two remove methods:
174:  
175:  Removing a string from the bag:
176:  remove() returns null
177:  The bag contains 0 string(s), as follows:
178:  
179:  
180:  Removing "B" from the bag:
181:  remove("B") returns false
182:  The bag contains 0 string(s), as follows:
183:  
184:  Adding to the bag: A B A C B C D
185:  The bag contains 7 string(s), as follows:
186:  A B A C B C D
187:  
188:  Removing strings:
189:  
190:  Testing the two remove methods:
191:  
192:  Removing a string from the bag:
193:  remove() returns D
194:  The bag contains 6 string(s), as follows:
195:  A B A C B C
196:  
197:  Removing "B" from the bag:
198:  remove("B") returns true
199:  The bag contains 5 string(s), as follows:
200:  A C A C B
201:  
202:  Removing "A" from the bag:
203:  remove("A") returns true
204:  The bag contains 4 string(s), as follows:
205:  B C A C
206:  
207:  Removing "C" from the bag:
208:  remove("C") returns true
209:  The bag contains 3 string(s), as follows:
210:  B C A
211:  
212:  Removing "Z" from the bag:
213:  remove("Z") returns false
214:  The bag contains 3 string(s), as follows:
215:  B C A
216:  
217:  Clearing the bag:
218:  
219:  Testing the method isEmpty with an empty bag:
220:  isEmpty finds the bag empty: OK.
221:  The bag contains 0 string(s), as follows:
222: */