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
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
