Question: . What five objects are created in the main? Can you spot the Comparator constructor call? Where is the class definition for the Comparator? 4

 . What five objects are created in the main? Can youspot the Comparator constructor call? Where is the class definition for the

. What five objects are created in the main? Can you spot the Comparator constructor call? Where is the class definition for the Comparator? 4 MyCompare3.java X Dog.java 1 import java.util.*; 2 3 class Animal { String name; 5 int legs; 6 70 Animal(String name, int legs) { 8 this.name = name; 9 this.legs = legs; 10 } 11 120 public int getLegs() { 13 return this. legs; 14 } 15 160 public String getName() { 17 return this.name; 18 } 19} 20 21 class Dog extends Animal{ 22 public Dog (String name, int legs) { 23 super(name, legs); 24 } 25 260 @Override A27 public String toString() { 28 String s = this.getName() + " has " + this.getLegs() + " legs"; 29 return s; 30 } 31 } 32 33 class MyClass { // Demo 340 public static void main(String... args) { 35 List dogs = new ArrayList(); 36 dogs.add(new Dog("Fido", 4)); 37 dogs.add(new Dog("Fido", 3)); 38 dogs.add(new Dog("Alfie", 4)); 39 40 System.out.println(dogs); 410 Collections.sort(dogs, new Comparator() { 420 @Override A43 public int compare(Animal a, Animal b) { 44 return a.getLegs() - b.getLegs(); 45 } 46 }); System.out.println(dogs); 48 } 49 } 47

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!