Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3843603 • Letter: C

Question

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

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
}
}

Explanation / Answer

The desired class:


import java.util.Comparator;

public class Toddler implements Comparable<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;
    }

    public int compareTo(Toddler compareToddler) {

        double compareQuantity = ((Toddler) compareToddler).getHeight();

        //ascending order
        //return this.quantity - compareQuantity;
        //descending order
        return compareQuantity - this.height;

    }
    public static Comparator<Toddler> ToddlerComparator
            = new Comparator<Toddler>() {

        public int compare(Toddler t1, Toddler t2) {

            String toddler1 = t1.getName().toUpperCase();
            String toddler2 = t2.getName().toUpperCase();

            //descending order
            return toddler2.compareTo(toddler1);
        }

    };
}