public class ArrayBagDemo1
1: /**
2: A test of the constructors and the methods add and toArray,
3: as defined in the first draft of the class ArrayBag.
4: @author Frank M. Carrano
5: @version 4.0
6: */
7: public class ArrayBagDemo1
8: {
9: public static void main(String[] args)
10: {
11: // Adding to an initially empty bag with sufficient capacity
12: System.out.println("Testing an initially empty bag with " +
13: " the capacity to hold at least 6 strings:");
14: BagInterface<String> aBag = new ArrayBag1<>();
15: String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
16: testAdd(aBag, contentsOfBag1);
17: // Filling an initially empty bag to capacity
18: System.out.println("\nTesting an initially empty bag that " +
19: " will be filled to capacity:");
20: aBag = new ArrayBag1<>(7);
21: String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D",
22: "another string"};
23: testAdd(aBag, contentsOfBag2);
24: } // end main
25:
26: // Tests the method add.
27: private static void testAdd(BagInterface<String> aBag,
28: String[] content)
29: {
30: System.out.print("Adding the following " + content.length +
31: " strings to the bag: ");
32: for (int index = 0; index < content.length; index++)
33: {
34: if (aBag.add(content[index]))
35: System.out.print(content[index] + " ");
36: else
37: System.out.print("\nUnable to add " + content[index] +
38: " to the bag.");
39: } // end for
40: System.out.println();
41:
42: displayBag(aBag);
43: } // end testAdd
44: // Tests the method toArray while displaying the bag.
45: private static void displayBag(BagInterface<String> aBag)
46: {
47: System.out.println("The bag contains the following string(s):");
48: Object[] bagArray = aBag.toArray();
49: for (int index = 0; index < bagArray.length; index++)
50: {
51: System.out.print(bagArray[index] + " ");
52: } // end for
53:
54: System.out.println();
55: } // end displayBag
56: } // end ArrayBagDemo1
57: /*
58: Testing an initially empty bag with sufficient capacity:
59: Adding the following 6 strings to the bag: A A B A C A
60: The bag contains the following string(s):
61: A A B A C A
62:
63: Testing an initially empty bag that will be filled to capacity:
64: Adding the following 8 strings to the bag: A B A C B C D
65: Unable to add another string to the bag.
66: The bag contains the following string(s):
67: A B A C B C D
68: */