-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecies.java
55 lines (54 loc) · 1.54 KB
/
Species.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
public class Species
implements Comparable<Species>{
private String commonName;
private int count;
private int highestPosition;
private int positionSum;
public Species(String commonName, int position){
this.commonName = commonName;
this.count = 1;
this.highestPosition = position;
this.positionSum = position;
}
public int getCount(){
return count;
}
public String getCommonName(){
return commonName;
}
public double getAveragePosition(){
return 1.0*this.positionSum/this.count;
}
public boolean addPosition(int position){
++this.count;
this.positionSum += position;
if(position > highestPosition){
this.highestPosition = position;
return true;
}
return false;
}
public int compareTo(Species other){
if(this.count < other.count) return 1;
if(this.count > other.count) return -1;
if((this.getAveragePosition()) < (other.getAveragePosition()))
return 1;
if((this.getAveragePosition()) > (other.getAveragePosition()))
return -1;
// if(this.highestPosition < other.highestPosition) return 1;
// if(this.highestPosition > other.highestPosition) return -1;
return 0;
}
public String toString(){
return commonName;
}
public boolean equals(Object other){
if (other == null){
return false;
}
if (this.getClass() != other.getClass()) {
return false;
}
return this.commonName.equals(((Species)other).getCommonName());
}
}