-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExercise3.java
More file actions
93 lines (61 loc) · 2.72 KB
/
Copy pathExercise3.java
File metadata and controls
93 lines (61 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package generics.exercises;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Exercise 3: Wildcards and PECS
*
* Tasks:
* 1. Write a method printList(List<?> list) that prints any list
* - Try with different types of lists
*
* 2. Write a method sumList(List<? extends Number> numbers) that calculates
* the sum of all numbers
* - Try with List<Integer>, List<Double>, and mixed List<Number>
*
* 3. Write a method addThreeIntegers(List<? super Integer> list) that adds
* the numbers 10, 20, 30 to the list
* - Try with List<Integer>, List<Number>, and List<Object>
*
* 4. Write a method copy(List<? extends T> source, List<? super T> dest)
* that copies all elements from source to destination
* - Demonstrate PECS principle
* - Try by copying List<Integer> to List<Number>
* - Try by copying List<Double> to List<Object>
*
* 5. Write a method maxOfTwo(List<? extends Number> list1,
* List<? extends Number> list2)
* that returns the maximum element from both lists combined
* - Try with different number types
*
* 6. BONUS: Create a method that demonstrates why you can't add to
* List<? extends Number> (producer)
* Write a comment explaining why
*/
public class Exercise3 {
public static void main(String[] args) {
System.out.println("=== Task 1: Print Any List ===\n");
// TODO: Call printList with different types
System.out.println("\n=== Task 2: Sum Numbers ===\n");
// TODO: Call sumList
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
List<Double> doubles = Arrays.asList(1.5, 2.5, 3.5);
System.out.println("\n=== Task 3: Add Integers ===\n");
// TODO: Call addThreeIntegers with different list types
List<Integer> intList = new ArrayList<>();
List<Number> numList = new ArrayList<>();
List<Object> objList = new ArrayList<>();
System.out.println("\n=== Task 4: Copy Lists (PECS) ===\n");
// TODO: Demonstrate PECS with copy method
System.out.println("\n=== Task 5: Max of Two Lists ===\n");
// TODO: Call maxOfTwo method
System.out.println("\n=== Task 6: BONUS - Why Can't Add? ===\n");
// TODO: Try to explain and demonstrate
}
// TODO: Task 1 - Implement printList
// TODO: Task 2 - Implement sumList
// TODO: Task 3 - Implement addThreeIntegers
// TODO: Task 4 - Implement copy method
// TODO: Task 5 - Implement maxOfTwo
// TODO: Task 6 - BONUS
}