Question: A Java question. You are given a CollegeWrestler class. A CollegeWrestler has a String name and a weight as a double. We need to order

A Java question. You are given a CollegeWrestler class. A CollegeWrestler has a String name and a weight as a double. We need to order wrestlers in two ways: alphabetically by name and by weight. We can not do this with the Comparable interface.

Create two Comparator objects, one to handle each type of ordering. You can do this several different ways. If you write separate classes, call then CollegeWrestlerComparatorByWeight and CollegeWrestlerComparatorByName. If you use inner classes or anonymous inner classes in ComparatorRunner, just leave the text area for the Comparator files empty.

Then complete the ComparatorRunner class. It defines an ArrayList of CollegeWrestlers.

Sort by weight and then loop through the ArrayList printing the weight and then the name of each CollegeWrestlers (use Collections.sort)

Sort by name and then loop through the ArrayList printing the name and then the weight of each CollegeWrestlers.(use Collections.sort)

The CollegeWrestler is given:

CollegeWrestler.java

public class CollegeWrestler { private String name; private double weight; public CollegeWrestler( String name, double weight) { this.name = name; this.weight = weight; } public double getWeight() { return weight; } public String getName() { return name; } } 

and complete the ComparatorRunner:

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;

public class ComparatorRunner {

public static void main(String[] args) { ArrayList wrestlers = new ArrayList(); wrestlers.add(new CollegeWrestler("Collin", 175.5)); wrestlers.add(new CollegeWrestler("Dong", 124.5)); wrestlers.add(new CollegeWrestler("Jose", 140.9)); wrestlers.add(new CollegeWrestler("Enrique", 290)); }

}

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!