Complete a classe Application por forma a implementar a funcionalidade desejada: ordenar a lista de gatos por idade (a implementação por omissão, para Animal, ordena por peso). Não é aceitável a alteração da classe Animal ou da classe Cat. Pode ser necessário criar classes auxiliares.
import java.util.*;
class Animal implements Comparable<Animal> {
private Double _weight;
public Animal(double weight) { _weight = weight; }
public Double getWeight() { return _weight; }
public int compareTo(Animal a) { return _weight.compareTo(a.getWeight()); }
public String toString() { return "Peso " + _weight; }
}
class Cat extends Animal {
private Integer _age;
public Cat(int age, double weight) { super(weight); _age = age; }
public Integer getAge() { return _age; }
public String toString() { return super.toString() + " e idade " + _age; }
}
public class Application {
public static void main(String args[]) {
List<Cat> cats = new ArrayList<Cat>();
cats.add(new Cat(1, 8));
cats.add(new Cat(2, 7));
cats.add(new Cat(3, 6));
Collections.sort(cats); // ordenação por peso (_weight)
for (Cat cat: cats) System.out.println(cat);
//... código em falta ... // ordenação por idade (_age)
for (Cat cat: cats) System.out.println(cat);
}
}