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