public class BeABag
1: import java.util.Random;
3: /**
4: *
5: * @author Mark Young (A00000000)
6: */
7: public class BeABag {
9: public static void main(String[] args) {
10: // Introduce yourself
11: System.out.print("\n\n"
12: + "Bag Testing Program\n"
13: + "-------------------\n\n"
14: + "This program tests an implementation of the Bag interface. "
15: + "The Bag in this\nprogram contains strings "
16: + "representing colour pencils.\n\n"
17: + "Now as it happens, "
18: + "we haven't actually programmed a Bag into the computer,\n"
19: + "so we're going to need your help. "
20: + "Please get a bunch of colour pencils and\na bag, "
21: + "and follow the instructions."
22: + "\n\n"
23: + "Thanks!"
24: + "\n\n");
25: // create the bag
26: Bag<String> helper = new HumanBag();
28: // test add
29: int numPencils = r.nextInt(6) + 5;
30: for (int i = 0; i < numPencils; ++i) {
31: System.out.println();
32: if (!helper.add(randomPencil())) {
33: System.out.println("Wow! That was unexpected....");
34: } else {
35: System.out.println("Good.");
36: }
37: }
38: System.out.println();
40: // run thru extra tests once with stuff in the bag
41: testBag(helper);
43: // test clear
44: helper.clear();
45: System.out.println();
46: testBag(helper);
47: }
49: private static void testBag(Bag<String> theBag) {
50: // test toArray
51: Object[] inBag = theBag.toArray();
52: System.out.println();
53: System.out.println("---------- Bag Contents ----------");
54: for (int i = 0; i < inBag.length; ++i) {
55: System.out.println(" inBag[" + i + "] is " + inBag[i]);
56: }
57: System.out.println("---------- End Contents ----------");
58: System.out.println();
60: // test isEmpty
61: if (!theBag.isEmpty()) {
62: System.out.println("The bag is not empty!");
63: } else {
64: System.out.println("How sad that the bag is empty!");
65: }
66: System.out.println();
68: // test getFrequencyOf
69: String testPencil = randomPencil();
70: int num = theBag.getFrequency(testPencil);
71: System.out.println("There are " + num + " "
72: + testPencil + "s in the bag!");
73: System.out.println();
75: // test contains
76: testPencil = randomPencil();
77: if (theBag.contains(testPencil)) {
78: System.out.println("Hooray!!!");
79: } else {
80: System.out.println("Boooo!!!");
81: }
82: System.out.println();
84: // test remove
85: String removed = theBag.remove();
86: if (removed != null) {
87: System.out.println("Thank you for the " + removed + " pencil.");
88: } else {
89: System.out.println("Thanks for NOTHIN'!");
90: }
91: System.out.println();
92: if (!theBag.remove(randomPencil())) {
93: System.out.println("HUMPH!");
94: } else {
95: System.out.println("Thank you.");
96: }
97: System.out.println();
99: // test getCurrentSize
100: System.out.println("There are " + theBag.size()
101: + " pencils in the bag!");
102: System.out.println();
103: }
105: private static String randomPencil() {
106: int pencilNum = r.nextInt(pencilSelection.length);
107: return pencilSelection[pencilNum];
108: }
110: private static final Random r = new Random();
111: private static final String[] pencilSelection
112: = new String[]{
113: "black", "white", "grey",
114: "red", "orange", "yellow", "green", "blue", "purple",
115: "brown", "pink"
116: };
118: }