-
Notifications
You must be signed in to change notification settings - Fork 48
/
ch03_LapTimes.java
57 lines (49 loc) · 1.79 KB
/
ch03_LapTimes.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
import java.util.ArrayList;
import java.util.List;
public class ch03_LapTimes {
static double totalTime(List<Double> lapTimes) {
lapTimes.remove(0); // remove warm-up lap
double sum = 0;
for (double x : lapTimes) {
sum += x;
}
return sum;
}
static double avgTime(List<Double> lapTimes) {
double time = totalTime(lapTimes);
int laps = lapTimes.size();
return time / laps;
}
static double totalTimePure(List<Double> lapTimes) { // named totalTime in the book
List<Double> withoutWarmUp = new ArrayList<>(lapTimes);
withoutWarmUp.remove(0); // remove warm-up lap
double sum = 0;
for (double x : withoutWarmUp) {
sum += x;
}
return sum;
}
static double avgTimePure(List<Double> lapTimes) { // named avgTime in the book
double time = totalTimePure(lapTimes); // named totalTime in the book
List<Double> withoutWarmUp = new ArrayList<>(lapTimes);
withoutWarmUp.remove(0); // remove warm-up lap
int laps = withoutWarmUp.size();
return time / laps;
}
public static void main(String[] args) {
ArrayList<Double> lapTimes = new ArrayList<>();
lapTimes.add(31.0);
lapTimes.add(20.9);
lapTimes.add(21.1);
lapTimes.add(21.3);
System.out.printf("Total: %.1fs\n", totalTime(lapTimes));
System.out.printf("Avg: %.1fs\n", avgTime(lapTimes));
ArrayList<Double> lapTimes2 = new ArrayList<>();
lapTimes2.add(31.0); // warm-up lap
lapTimes2.add(20.9);
lapTimes2.add(21.1);
lapTimes2.add(21.3);
System.out.printf("Total: %.1fs\n", totalTimePure(lapTimes2));
System.out.printf("Avg: %.1fs", avgTimePure(lapTimes2));
}
}