public class TestClassWithList
1: import java.util.List;
2: import java.util.Scanner;
4: /**
5: *
6: * @author Mark Young (A00000000)
7: */
8: public class TestClassWithList {
9:
10: private static final Scanner KBD = new Scanner(System.in);
12: /**
13: * @param args the command line arguments
14: */
15: public static void main(String[] args) {
16: ClassWithList myObject = new ClassWithList("Primes");
17:
18: for (int i : new int[]{2, 3, 5, 7, 11, -4, 13}) {
19: try {
20: myObject.add(i);
21: System.out.println("Added " + i);
22: } catch (IllegalArgumentException iae) {
23: System.out.println("Could not add " + i);
24: }
25: }
26: System.out.println(myObject);
27: pause();
28:
29: // use GOOD method
30: System.out.println("Getting the numbers using a SAFE method");
31: List<Integer> theNumbers = myObject.getNumbersSafely();
32: System.out.println("The List I got: " + theNumbers);
33: System.out.println("Going to try to add -77 using the SAFE list");
34: try {
35: theNumbers.add(-77);
36: System.out.println("It worked!");
37: } catch (UnsupportedOperationException uso) {
38: System.out.println("That failed");
39: }
40: System.out.println("Going to try to change first number to -77");
41: try {
42: theNumbers.set(0, -77);
43: System.out.println("It worked!");
44: } catch (UnsupportedOperationException uso) {
45: System.out.println("That failed");
46: }
47: System.out.println("Going to try to delete the number at index 2");
48: try {
49: theNumbers.remove(2);
50: System.out.println("It worked!");
51: } catch (UnsupportedOperationException uso) {
52: System.out.println("That failed");
53: }
54: System.out.println("After all that...");
55: System.out.println(myObject);
56: pause();
57:
58: // use BAD method
59: System.out.println("Getting the numbers using the UNSAFE method");
60: theNumbers = myObject.getNumbersForCheater();
61: System.out.println("The List I got: " + theNumbers);
62: System.out.println("Going to try to add -77");
63: try {
64: theNumbers.add(-77);
65: System.out.println("It worked!");
66: } catch (UnsupportedOperationException uso) {
67: System.out.println("That failed");
68: }
69: System.out.println("Going to try to change first number to -77");
70: try {
71: theNumbers.set(0, -77);
72: System.out.println("It worked!");
73: } catch (UnsupportedOperationException uso) {
74: System.out.println("That failed");
75: }
76: System.out.println("Going to try to delete the number at index 2");
77: try {
78: theNumbers.remove(2);
79: System.out.println("It worked!");
80: } catch (UnsupportedOperationException uso) {
81: System.out.println("That failed");
82: }
83: System.out.println("After all that...");
84: System.out.println(myObject);
85: pause();
86: }
88: private static void pause() {
89: System.out.println();
90: System.out.print("...press enter...");
91: KBD.nextLine();
92: System.out.println();
93: }
95: }