-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticInnerClass.java
74 lines (68 loc) · 1.85 KB
/
StaticInnerClass.java
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
package staticInnerClass;
/**
* This program demonstrates the use of static inner classes.
*
* @author Cay Horstmann
* @version 1.02 2015-05-12
*/
public class StaticInnerClass {
public static void main(String[] args) {
double[] d = new double[20];
for (int i = 0; i < d.length; i++) {
d[i] = 100 * Math.random();
}
ArrayAlg.Pair pair = ArrayAlg.minmax(d);
System.out.println("min = " + pair.getFirst());
System.out.println("max = " + pair.getSecond());
}
}
class ArrayAlg {
/**
* Computes both the minimum, and the maximum of an array
*
* @param values an array of floating-point numbers
* @return pair whose first element is the minimum and whose second element
*/
public static Pair minmax(double[] values) {
double min = Double.POSITIVE_INFINITY;
double max = Double.POSITIVE_INFINITY;
for (double v : values) {
if (min > v) min = v;
if (max < v) max = v;
}
return new Pair(min, max);
}
/**
* A pair of floating-point numbers
*/
public static class Pair {
private final double first;
private final double second;
/**
* Constructs a pair from two floating-point numbers
*
* @param f the first number
* @param s the second number
*/
public Pair(double f, double s) {
first = f;
second = s;
}
/**
* Returns the first number of the pair
*
* @return the first number
*/
public double getFirst() {
return first;
}
/**
* Returns the second number of the pair
*
* @return the second number
*/
public double getSecond() {
return second;
}
}
}