Repository navigation
Expand file tree
/
Copy pathVoter.java
More file actions
67 lines (51 loc) · 1.71 KB
/
Copy pathVoter.java
File metadata and controls
67 lines (51 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Voter {
private double[] beliefs = new double[16]; //a voter's beliefs are represented by an Array of values ranging from 0.0 to 1.0, thereby constituting an ideology vector
private String name;
public String getName() {
return name;
}
public Voter(double[] beliefs) {
this.beliefs = beliefs;
}
public Voter(boolean placeholder) {
for (int i = 0; i < beliefs.length; i++) {
beliefs[i] = Math.random();
}
}
public Voter() {
for (int i = 0; i < beliefs.length; i++) {
beliefs[i] = Math.random();
}
try {
name = scanFile().get((int) (Math.random() * 1001));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public double[] getBeliefs() {
return beliefs;
}
public static double calculateIdeologicalDistance(Voter v1, Voter v2) {
double distance = 0.0;
for (int i = 0; i < v1.getBeliefs().length; i++) {
distance += Math.pow(v1.getBeliefs()[i] -v2.getBeliefs()[i], 2);
}
return Math.abs(Math.sqrt(distance));
}
private static ArrayList<String> scanFile() throws FileNotFoundException {
File file = new File("last_names.txt");
Scanner input = new Scanner(file);
ArrayList<String> names = new ArrayList<String>();
while(input.hasNext()) {
String nextLine = input.nextLine();
names.add(nextLine);
}
input.close();
return names;
}
}