-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordObject.java
52 lines (42 loc) · 1.36 KB
/
wordObject.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
public class wordObject implements Comparable<wordObject>{
int fittness;
private String word;
int appeal;//compared to other objects
boolean matecapable;
//constructor.
public wordObject(String word){
this.word = word;
this.fittness = compareforfittness(this.word, GeneticAlgorithm.goal);
}
//gets fittness of word passed thorugh
public static int compareforfittness(String word, String goal){
int fittness = 0;
char[] first = word.toCharArray();
char[] second = goal.toCharArray();
//comparison
for(int i =0;i<word.length();i++){
if (first[i] == second[i]){
fittness++;
}
}
return fittness;
}
//getter for word.
public String getword() {
return word;
}
//getter for word.
public int getFittness(){
return fittness;
}
//getter and setter for word and fittness.
public void setWord(String newword){
this.word = newword;
this.fittness = compareforfittness(this.word, GeneticAlgorithm.goal);
}
//comparable method for creating sorted array(very important).
public int compareTo(wordObject other) {
int otherFittness = ((wordObject) other).getFittness();
return otherFittness - this.fittness;
}
}