public class Pair<F,S> implements Comparable<Pair<F,S>> {
public F first;
public S second;
public F first() {
return first;
}
public void setFirst(F first) {
this.first=first;
}
public S second() {
return second;
}
public void setSecond(S second) {
this.second=second;
}
public Pair(F first, S second) {
super();
this.first=first;
this.second=second;
}
public int hashCode() {
return(first.hashCode()^second.hashCode());
}
@Override
public boolean equals(Object obj) {
return obj instanceof Pair && ((Pair)obj).first.equals(first) && (Pair)obj).second.equals(second);
}
public String toString() {
return first + " / " + second;
}
@SuppressWarnings("unchecked")
public int compareTo(Pair<F, S> o) throws ClassCastException{
int firstCompared = ((Comparable<F>) first).compareTo(o.first());
if(firstCompared!=0) return(firstCompared);
return(((Comparable<S>)second).compareTo(o.second()));
}
}
and and I have the following class:
public class Point{
public int x;
public int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public String toString(){
return "(" + x + "," + y + ")";
}
}
My Question: Suppose i have four points p1, p2, p3, p3. How can i use the Pair class to compare the pair (p1, p2) with (p2,p3)? How can i use the compareTo function? Thank you