// Hunter Schafer, CSE 143 // A class representing a Cat object public class Cat implements Comparable { private String name; private int lives; private double weight; // Constructs a new cat with the given name, number of lives and weight public Cat(String name, int lives, double weight) { this.name = name; this.lives = lives; this.weight = weight; } // Returns a number < 0 if this Cat comes before other // Returns a number > 0 if this Cat comes after other // Returns 0 if this Cat and the other are the same // Cats are ordered first by lives, then weight, then name public int compareTo(Cat other) { // order by lives, then weight, then name if (this.lives != other.lives) { // sort by lives return this.lives - other.lives; // only works for integers } else if (this.weight != other.weight) { // sort by weight if (this.weight < other.weight) { return -1; } else { // this.weight > other.weight return 1; } } else { // lives are equal, weights are equal, sort by name return this.name.compareTo(other.name); } } // Returns the String representation of this cat public String toString() { return "(^=.=^)" + name + " {" + lives + ", " + weight + "}"; } }