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