-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPair.java
More file actions
53 lines (39 loc) · 1.05 KB
/
Pair.java
File metadata and controls
53 lines (39 loc) · 1.05 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
package Pair;
/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* getFirst
* getSecond
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair<E extends Comparable> {
private E first;
private E second;
public Pair(E first, E second) {
this.first = first;
this.second = second;
}
public E getFirst() {
return this.first;
}
public E getSecond() {
return this.second;
}
@SuppressWarnings("unchecked")
public E min() {
if (first.compareTo(second) > 0) {
return second;
}
return first;
}
@SuppressWarnings("unchecked")
public E max() {
if (first.compareTo(second) < 0) {
return second;
}
return first;
}
}
//Pair -- This is a multi-step one:
// Create a Pair that stores a pair of elements of type E.
// Create two methods, min and max, that return the largest and smallest of the Pair.