-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.java
55 lines (51 loc) · 2.07 KB
/
Tests.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
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.Math;
public class Tests{
public static void main(String[] args) {
ArrayList<Double> targetIndices = new ArrayList<Double>();
targetIndices.add(2.00);
targetIndices.add(4.00);
System.out.println("Test 1 Distance.....");
System.out.println(distance(targetIndices, 1.00));
System.out.println("Should be 1");
System.out.println(distance(targetIndices, 7));
System.out.println("Should be 3");
ArrayList<Double> targetIndices2 = new ArrayList<Double>();
targetIndices2.add(2.00);
targetIndices2.add(5.00);
System.out.println(distance(targetIndices2, 3));
System.out.println("Should be 1.0");
System.out.println(distance(targetIndices2, 4));
System.out.println("Should be 1.0");
ArrayList<Double> targetIndices3 = new ArrayList<Double>();
targetIndices3.add(0.00);
targetIndices3.add(7.00);
System.out.println(distance(targetIndices3, 3));
System.out.println("Should be 3.0");
System.out.println(distance(targetIndices3, 4));
System.out.println("Should be 3.0");
ArrayList<Double> targetIndices4 = new ArrayList<Double>();
targetIndices4.add(2.00);
targetIndices4.add(3.00);
System.out.println("Test 4 Distance.....");
System.out.println(distance(targetIndices4, 4));
System.out.println("Should be 1.0");
System.out.println(distance(targetIndices4, 5));
System.out.println("Should be 2.0");
}
private static double distance(ArrayList<Double> targetIndices, double currentIndex) {
double minDistance = Double.POSITIVE_INFINITY;
for (int i=0; i<targetIndices.size(); i++) {
double current = Math.abs(targetIndices.get(i) - currentIndex);
if (current < minDistance) {
minDistance = current;
}
}
return minDistance;
}
}