-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsWildCard.java
More file actions
41 lines (40 loc) · 1.35 KB
/
Copy pathGenericsWildCard.java
File metadata and controls
41 lines (40 loc) · 1.35 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
// Use a wildcard.
class NumericFnsWC<T extends Number> {
T num;
// Pass the constructor a reference to a numeric object.
NumericFnsWC(T n) {
num = n;
}
// Return the reciprocal.
double reciprocal() {
return 1 / num.doubleValue();
}
// Return the fractional component.
double fraction() {
return num.doubleValue() - num.intValue();
}
// Determine if the absolute values of two objects are the same.
boolean absEqual(NumericFnsWC<?> ob) {
if(Math.abs(num.doubleValue()) ==
Math.abs(ob.num.doubleValue())) return true;
return false;
}
}
public class GenericsWildCard {
public static void main(String[] args) {
NumericFnsWC<Integer> iOb = new NumericFnsWC<Integer>(6);
NumericFnsWC<Double> dOb = new NumericFnsWC<Double>(-6.0);
NumericFnsWC<Long> lOb = new NumericFnsWC<Long>(5L);
System.out.println("Testing iOb and dOb.");
if(iOb.absEqual(dOb))
System.out.println("Absolute values are equal.");
else
System.out.println("Absolute values differ.");
System.out.println();
System.out.println("Testing iOb and lOb.");
if(iOb.absEqual(lOb))
System.out.println("Absolute values are equal.");
else
System.out.println("Absolute values differ.");
}
}