KaiquanMah commited on
Commit
a679cd0
·
verified ·
1 Parent(s): 94eab9a

class MinMax<T extends Comparable<T>>

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/07B. Generic Class MinMax???? ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a generic class
2
+
3
+ class MinMax<T extends Comparable<T>>
4
+ ...where the implementing type must implement the Comparable interface.
5
+
6
+ The class has the following properties:
7
+ 1
8
+ A constructor that takes a list as a parameter. The list contains elements of type T.
9
+ 2
10
+ A method public T smallest(), which returns the smallest element in the list.
11
+ 3
12
+ A method public T largest(), which returns the largest element in the list.
13
+ 4
14
+ The comparison of list elements uses the services provided by the Comparable interface.
15
+
16
+ Tip: Check out the Collections class API description - there are a couple of useful operations for finding the smallest or largest element...
17
+ https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+ import java.util.Random;
26
+ import java.util.ArrayList;
27
+ import java.util.Collections;
28
+
29
+ public class Test {
30
+ public static void main(String[] args) {
31
+ final Random r = new Random();
32
+
33
+ System.out.println("Testing the MinMax class...");
34
+
35
+ System.out.println("Testing with integers");
36
+ int n = -r.nextInt(200);
37
+ ArrayList<Integer> numbers = new ArrayList<>();
38
+
39
+ int count = r.nextInt(10) + 10;
40
+ for (int i = 0; i < count; i++) {
41
+ numbers.add(n);
42
+ n += r.nextInt(40);
43
+ }
44
+
45
+ Collections.shuffle(numbers, r);
46
+
47
+ System.out.println("List:" + numbers);
48
+ System.out.println("Creating object...");
49
+
50
+ MinMax<Integer> mm = new MinMax<>(numbers);
51
+ System.out.println("Object created!");
52
+
53
+ System.out.println("Smallest: " + mm.smallest());
54
+ System.out.println("Largest: " + mm.largest());
55
+
56
+ System.out.println("");
57
+
58
+ System.out.println("Testing with floating-point numbers");
59
+ double n2 = -r.nextInt(240);
60
+ ArrayList<Double> numbers2 = new ArrayList<>();
61
+
62
+ Double[] d = {0.0, 0.25, 0.5, 0.75};
63
+
64
+ count = r.nextInt(10) + 10;
65
+ for (int i = 0; i < count; i++) {
66
+ n2 += r.nextInt(40) + d[r.nextInt(d.length)];
67
+ numbers2.add(n2);
68
+ }
69
+
70
+ Collections.shuffle(numbers2, r);
71
+
72
+ System.out.println("List:" + numbers2);
73
+ System.out.println("Creating object...");
74
+
75
+ MinMax<Double> mm2 = new MinMax<>(numbers2);
76
+ System.out.println("Object created!");
77
+
78
+ System.out.println("Smallest: " + mm2.smallest());
79
+ System.out.println("Largest:" + mm2.largest());
80
+
81
+ }
82
+ }
83
+
84
+
85
+
86
+
87
+ //ADD
88
+ class MinMax<T extends Comparable<T>> {
89
+ private ArrayList<T> list;
90
+
91
+ public MinMax(ArrayList<T> list) {
92
+ this.list = list;
93
+ }
94
+
95
+ public T smallest() {
96
+ return Collections.min(list);
97
+ }
98
+
99
+ public T largest() {
100
+ return Collections.max(list);
101
+ }
102
+ }
103
+
104
+
105
+
106
+
107
+
108
+ Testing the MinMax class...
109
+ Testing with integers
110
+ List:[104, 197, 159, 178, 106, 164, 33, -20, 11, 47, 130, 209, 84, 107, 198, -51]
111
+ Creating object...
112
+ Object created!
113
+ Smallest: -51
114
+ Largest: 209
115
+
116
+ Testing with floating-point numbers
117
+ List:[207.5, 6.0, 151.0, 256.75, 115.5, 205.0, 292.75, 24.25, 180.25, 83.0, 74.0, 6.0, 41.0, 33.75, 231.0]
118
+ Creating object...
119
+ Object created!
120
+ Smallest: 6.0
121
+ Largest:292.75
122
+