Question: java import java.util.ArrayList; import java.util.Collections; abstract class Pets { String name; String color; int age; int legs; boolean hair; public Pets(String name, String color, int

java

import java.util.ArrayList; import java.util.Collections; abstract class Pets { String name; String color; int age; int legs; boolean hair; public Pets(String name, String color, int age, int legs, boolean hair) { this.name = name; this.color = color; this.age = age; this.legs = legs; this.hair = hair; } public abstract void speak(); public abstract void move(); } class Dog extends Pets implements Comparable { public Dog(String name, String color, int age, int legs, boolean hair) { super(name, color,age, legs, hair); } public void speak() { System.out.println("The dog is speaking!"); } public void move() { System.out.println("The dog is moving!"); } public String getName() { return name; } public void getInfo() { System.out.print(name + " " + color + " " + age + " " ); if(hair == true) { System.out.println("Hair"); } else System.out.println("Hairless"); }

@Override public int compareTo(Object o) { Dog d = (Dog) o; return this.name.compareTo(d.getName()); }

}

public class HW_AP { public static void main(String[] args) { ArrayList dogs=new ArrayList(); Dog dog1 = new Dog("A", "99", 10, 4, true); Dog dog2 = new Dog("GLA", "99", 10, 4, false); Dog dog3 = new Dog("AB", "99", 10, 4, false); Dog dog4 = new Dog("BA", "99", 10, 4, true); Dog dog5 = new Dog("CCA", "99", 10, 4, false); dogs.add(dog1); dogs.add(dog2); dogs.add(dog3); dogs.add(dog4); dogs.add(dog5); System.out.println("Before sorting"); System.out.println("DogName Color Age Hair/Hairless"); for(Dog i:dogs) { i.getInfo(); } System.out.println("After sorting"); System.out.println("DogName Color Age Hair/Hairless"); Collections.sort(dogs); for(Dog i:dogs) { i.getInfo(); }

}}

java import java.util.ArrayList; import java.util.Collections; abstract class Pets { String name; String

REWORKING THE JOURNAL. Go back to last weeks assignment and see if you can perfect the Sort with the comparable interface. Now, Sort the List by First: Hair or Hairless Second: Age Example: Before Sort Dog Age Hair? Lad 10 True Lassie 5 True Sparkie 10 False Lassie 5 False Zeb 3 False After Sort Dog Age Hair? Zeb 3 False Lassie 5 False Sparkle 10 False Lassie 5 True Lad 10 True (HINT: Remember, the sort rules are all programmed in the compareTo() method ) Nothing changes in the Collections.Sort()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!