public class ArrayBagDemo
1: /** A demonstration of the class ArrayBag
2: @author Frank M. Carrano
3: @version 4.0
4: */
5: public class ArrayBagDemo
6: {
7: public static void main(String[] args)
8: {
9: String[] contentsOfBag = {"A", "A", "B", "A", "C", "A"};
10: // Tests on an empty bag
11: BagInterface<String> aBag = new ArrayBag<>(contentsOfBag.length);
12: System.out.println("Testing an initially empty bag:");
13: testIsEmpty(aBag, true);
14: String[] testStrings1 = {"", "B"};
15: testFrequency(aBag, testStrings1);
16: testContains(aBag, testStrings1);
17: testRemove(aBag, testStrings1);
18: // Adding strings
19: System.out.println("Adding " + contentsOfBag.length +
20: " strings to an initially empty bag with" +
21: " the capacity to hold more than " +
22: contentsOfBag.length + " strings:");
23: testAdd(aBag, contentsOfBag);
24:
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: // Removing strings
32: String[] testStrings3 = {"", "B", "A", "C", "Z"};
33: testRemove(aBag, testStrings3);
34: System.out.println("\nClearing the bag:");
35: aBag.clear();
36: testIsEmpty(aBag, true);
37: displayBag(aBag);
38:
39: // Filling an initially empty bag to capacity
40: System.out.println("\nTesting an initially empty bag that " +
41: " will be filled to capacity:");
42: aBag = new ArrayBag<String>(7);
43: String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
44: testAdd(aBag, contentsOfBag2);
45:
46: System.out.println("Try to add another string to the full bag:");
47: if (aBag.add("another string"))
48: System.out.println("Added a string beyond the bag's capacity: ERROR!");
49: else
50: System.out.println("The method add cannot add another string: OK");
51: } // end main
52:
53: // Tests the method add.
54: private static void testAdd(BagInterface<String> aBag, String[] content)
55: {
56: System.out.print("Adding ");
57: for (int index = 0; index < content.length; index++)
58: {
59: aBag.add(content[index]);
60: System.out.print(content[index] + " ");
61: } // end for
62: System.out.println();
63:
64: displayBag(aBag);
65: } // end testAdd
66: // Tests the two remove methods.
67: private static void testRemove(BagInterface<String> aBag, String[] tests)
68: {
69: for (int index = 0; index < tests.length; index++)
70: {
71: String aString = tests[index];
72: if (aString.equals("") || (aString == null))
73: {
74: // test remove()
75: System.out.println("\nRemoving a string from the bag:");
76: String removedString = aBag.remove();
77: System.out.println("remove() returns " + removedString);
78: }
79: else
80: {
81: // test remove(aString)
82: System.out.println("\nRemoving \"" + aString + "\" from the bag:");
83: boolean result = aBag.remove(aString);
84: System.out.println("remove(\"" + aString + "\") returns " + result);
85: } // end if
86:
87: displayBag(aBag);
88: } // end for
89: } // end testRemove
90: // Tests the method isEmpty.
91: // correctResult indicates what isEmpty should return.
92: private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult)
93: {
94: System.out.print("Testing isEmpty with ");
95: if (correctResult)
96: System.out.println("an empty bag:");
97: else
98: System.out.println("a bag that is not empty:");
99:
100: System.out.print("isEmpty finds the bag ");
101: if (correctResult && aBag.isEmpty())
102: System.out.println("empty: OK.");
103: else if (correctResult)
104: System.out.println("not empty, but it is empty: ERROR.");
105: else if (!correctResult && aBag.isEmpty())
106: System.out.println("empty, but it is not empty: ERROR.");
107: else
108: System.out.println("not empty: OK.");
109: System.out.println();
110: } // end testIsEmpty
111: // Tests the method getFrequencyOf.
112: private static void testFrequency(BagInterface<String> aBag, String[] tests)
113: {
114: System.out.println("\nTesting the method getFrequencyOf:");
115: for (int index = 0; index < tests.length; index++)
116: {
117: String aString = tests[index];
118: if (!aString.equals("") && (aString != null))
119: {
120: System.out.println("In this bag, the count of " + tests[index] +
121: " is " + aBag.getFrequencyOf(tests[index]));
122: } // end if
123: } // end for
124: } // end testFrequency
125:
126: // Tests the method contains.
127: private static void testContains(BagInterface<String> aBag, String[] tests)
128: {
129: System.out.println("\nTesting the method contains:");
130: for (int index = 0; index < tests.length; index++)
131: {
132: String aString = tests[index];
133: if (!aString.equals("") && (aString != null))
134: {
135: System.out.println("Does this bag contain " + tests[index] +
136: "? " + aBag.contains(tests[index]));
137: } // end if
138: } // end for
139: } // end testContains
140: // Tests the method toArray while displaying the bag.
141: private static void displayBag(BagInterface<String> aBag)
142: {
143: System.out.println("The bag contains " + aBag.getCurrentSize() +
144: " string(s), as follows:");
145: Object[] bagArray = aBag.toArray();
146: for (int index = 0; index < bagArray.length; index++)
147: {
148: System.out.print(bagArray[index] + " ");
149: } // end for
150:
151: System.out.println();
152: } // end displayBag
153: } // end ArrayBagDemo
154: /*
155: Testing an initially empty bag:
156: Testing isEmpty with an empty bag:
157: isEmpty finds the bag empty: OK.
158:
159:
160: Testing the method getFrequencyOf:
161: In this bag, the count of B is 0
162:
163: Testing the method contains:
164: Does this bag contain B? false
165:
166: Removing a string from the bag:
167: remove() returns null
168: The bag contains 0 string(s), as follows:
169:
170:
171: Removing "B" from the bag:
172: remove("B") returns false
173: The bag contains 0 string(s), as follows:
174:
175: Adding 6 strings to an initially empty bag with the capacity to hold more than 6 strings:
176: Adding A A B A C A
177: The bag contains 6 string(s), as follows:
178: A A B A C A
179: Testing isEmpty with a bag that is not empty:
180: isEmpty finds the bag not empty: OK.
181:
182:
183: Testing the method getFrequencyOf:
184: In this bag, the count of A is 4
185: In this bag, the count of B is 1
186: In this bag, the count of C is 1
187: In this bag, the count of D is 0
188: In this bag, the count of Z is 0
189:
190: Testing the method contains:
191: Does this bag contain A? true
192: Does this bag contain B? true
193: Does this bag contain C? true
194: Does this bag contain D? false
195: Does this bag contain Z? false
196:
197: Removing a string from the bag:
198: remove() returns A
199: The bag contains 5 string(s), as follows:
200: A A B A C
201:
202: Removing "B" from the bag:
203: remove("B") returns true
204: The bag contains 4 string(s), as follows:
205: A A C A
206:
207: Removing "A" from the bag:
208: remove("A") returns true
209: The bag contains 3 string(s), as follows:
210: A A C
211:
212: Removing "C" from the bag:
213: remove("C") returns true
214: The bag contains 2 string(s), as follows:
215: A A
216:
217: Removing "Z" from the bag:
218: remove("Z") returns false
219: The bag contains 2 string(s), as follows:
220: A A
221:
222: Clearing the bag:
223: Testing isEmpty with an empty bag:
224: isEmpty finds the bag empty: OK.
225:
226: The bag contains 0 string(s), as follows:
227:
228:
229: Testing an initially empty bag that will be filled to capacity:
230: Adding A B A C B C D
231: The bag contains 7 string(s), as follows:
232: A B A C B C D
233: Try to add another string to the full bag:
234: The method add cannot add another string: OK
235: */
236: /*
237: Exception in thread "main" java.lang.SecurityException: Attempt to create a bag whose capacity exceeds 3
238: at ArrayBag.<init>(ArrayBag.java:38)
239: at ArrayBagDemo.main(ArrayBagDemo.java:12)
240:
241: */