Using JAVA
C: Programming II
Consider the Box class.
public class Box implements Comparable<Box> {
private String a;
private String b;
public Box(String a, String b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Box otherBox) {
if (this.a.compareTo(otherBox.a) < 0) {
return -1;
} else if (this.a.compareTo(otherBox.a) > 0) {
return 1;
} else {
int length1 = this.a.length() + this.b.length();
int length2 = otherBox.a.length() + otherBox.b.length();
if (length1 < length2) {
return -1;
} else if (length1 > length2) {
return 1;
} else {
return 0;
}
}
}
}