Source of ArrayBagDemo2.java


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