public class ListErrors
1: import java.util.ArrayList;
2: import java.util.LinkedList;
3: import java.util.List;
4: import java.util.Scanner;
5: import java.util.function.Consumer;
6: import java.util.function.Function;
8: /**
9: *
10: * @author Mark Young (A00000000)
11: */
12: public class ListErrors {
13:
14: public static final Scanner KBD = new Scanner(System.in);
16: /**
17: * @param args the command line arguments
18: */
19: public static void main(String[] args) {
20: showOutOfBoundsErrors();
21: showTrueFalseNullReturns();
22: showUnsupportedOperations();
23: }
24:
25: private static void showOutOfBoundsErrors() {
26: List<String> myList = new ArrayList<>();
27: myList.addAll(List.of("S", "O", "M", "E"));
28:
29: System.out.println("Consider the list " + myList + ".");
30: System.out.println("Any call with an out-of-bounds index...");
31: checkThrowF("get(-1) ", myList, l -> l.get(-1));
32: checkThrowF("get(4) ", myList, l -> l.get(4));
33: checkThrowC("remove(-1)", myList, l -> l.remove(-1));
34: checkThrowC("remove(5) ", myList, l -> l.remove(5));
35: checkThrowC("add(-1, _)", myList, l -> l.add(-1, "D"));
36: checkThrowC("add(65, _)", myList, l -> l.add(65, "D"));
37: System.out.println("...throws an IndexOutOfBoundsException");
38: pause();
39: }
40:
41: private static void showTrueFalseNullReturns() {
42: List<String> myList = new LinkedList<>();
43: myList.addAll(List.of("S", "O", "M", "E"));
44:
45: System.out.println("Consider the list " + myList + ".");
46: System.out.println("Some operations return a special value...");
47: checkThrowF("remove(\"S\")", myList, l -> l.remove("S"));
48: checkThrowF("remove(\"A\")", myList, l -> l.remove("A"));
49: checkThrowF("add(\"R\") ", myList, l -> l.add("R"));
50: System.out.println("...true/false for succees/fail");
51: pause();
52: }
53:
54: private static void showUnsupportedOperations() {
55: List<String> myList = List.of("A", "B", "C");
56:
57: System.out.println("Consider List.of(\"A\", \"B\", \"C\").");
58: System.out.println("Any operations to change this list...");
59: checkThrowF("add(\"D\") ", myList, l -> l.add("D"));
60: checkThrowF("set(1, \"D\")", myList, l -> l.set(1, "D"));
61: checkThrowF("remove(0) ", myList, l -> l.remove(0));
62: System.out.println("... is just not allowed.");
63: }
64:
65: /**
66: * Catch and report on any exception that gets thrown by the given
67: * value-returning-method call.
68: *
69: * @param <T> the type of the object given
70: * @param list the object given (not necessarily a list)
71: * @param func the function to call on that object
72: */
73: private static <T, U> void checkThrowF(
74: String label, T list, Function<T, U> func) {
75: try {
76: System.out.print(" - " + label);
77: U result = func.apply(list);
78: System.out.println(" returned " + result);
79: } catch (Throwable thrown) {
80: System.out.println(" threw " + thrown);
81: }
82: }
84: /**
85: * Catch and report on any exception that gets thrown by the given
86: * non-value-returning-method call.
87: *
88: * @param <T> the type of the object given
89: * @param list the object given (not necessarily a list)
90: * @param cons the procedure to call on that object
91: */
92: private static <T> void checkThrowC(
93: String label, T list, Consumer<T> cons) {
94: try {
95: System.out.print(" - " + label);
96: cons.accept(list);
97: System.out.println(" ran to completion(!)");
98: } catch (Throwable thrown) {
99: System.out.println(" threw " + thrown);
100: }
101: }
103: private static void pause() {
104: System.out.println();
105: System.out.print("...press enter...");
106: KBD.nextLine();
107: System.out.println();
108: }
110: }