// Jeremy Lipschutz // This class represents a student's pet! public class StudentPet implements Comparable { private String name; private String type; private int age; private double weight; // Constructs a StudentPet with the given name, type, age, and weight public StudentPet(String name, String type, int age, double weight) { this.name = name; this.type = type; this.age = age; this.weight = weight; } // returns a String representation of this StudentPet public String toString() { return "\n" + " / \\__\n" + " ( @\\___ name:" + name + "\n" + " / O type:" + type + "\n" + " / (_____/ age:" + age + "\n" + "/_____/ U weight: " + weight + "\n"; } // this.compareTo(other) < 0, our pet is "less than" other // this.compareTo(other) > 0, our pet is "greater than" other // this.compareTo(other) == 0, our pet is "equal to" other // Compares this StudentPet against the given StudentPet, comparing on // age, name, weight, and type in that order of priority public int compareTo(StudentPet other) { int cmp = Integer.compare(this.age, other.age); if(cmp == 0) { cmp = this.name.compareTo(other.name); } if(cmp == 0) { cmp = Double.compare(this.weight, other.weight); } if(cmp == 0) { cmp = this.type.compareTo(other.type); } return cmp; } }