Question: Complete the class ComparingToddlers . It defines an array of Toddlers . First print the names of the Toddlers in the array in descending order

Complete the class ComparingToddlers. It defines an array of Toddlers.

First print the names of the Toddlers in the array in descending order by weight and then again in descending order by height. Use the Comparator interface to create the Comparator objects to pass to the sort method.

A Toddler class is included for you in Codecheck. Its constrcutor takes 2 parameters: name, height, weight

------------------------------------------------------------------------------------------------------

Use the following file:

Toddler.java

/** * Models a toddler - a one to three year * old child */ public class Toddler { private String name; private double height; private int weight; /** * Constructs a Toddler with the given parameters * @param name the name of this Toddler * @param height the height of this Toddler * @param weight the weight of this Toddler */ public Toddler(String name, double height, int weight) { this.name = name; this.height = height; this.weight = weight; } /** * Gets the name of this Toddler * @return the name of this Toddler */ public String getName() { return name; } /** * Gets the height of this Toddler * @return the height of this Toddler */ public double getHeight() { return height; } /** * Gets the weight of this Toddler * @return the weight of this Toddler */ public int getWeight() { return weight; } @Override public String toString() { String s = "Toddler[name=" + name + ",height=" + height + ",weight=" + weight +"]"; return s; } } 

complete the following file:

ComparingToddlers.java

public class ComparingToddlers {

public static void main(String[] args) { Toddler[] kids = { new Toddler("Anwen", 42.0, 47), new Toddler("Taran", 36.0, 40), new Toddler("Jose", 36.0, 50), new Toddler("Jaime", 40.5, 39), new Toddler("Jaime", 40.5, 42), new Toddler("Yasmin", 36.0, 41) };

System.out.println("**Sort by weight"); //print the array sorted by weight here System.out.println("**Sort by height"); //print the array sorted by height here } }

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!