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

Most Valuable Player Purpose To review classes and interfaces that use generics

ID: 3830415 • Letter: M

Question

Most Valuable Player Purpose To review classes and interfaces that use generics Directions Your task is to develop an Athlete class that has the person's name, sport, and ranking, where the name and sport are Strings and the ranking is anything that is comparable (do NOT hardcode this to an int use generics). You should be able to sort athletes two different ways: alphabetically by sportand if two athletes have the same sport, they should be sorted alphabetically by name or alphabetically by sport and if two athletes have the same sport, they should be sorted in decreasing order of rank. In your main method, create five Athlete objects, add them to an ArrayList, and display them. Then sort the ArrayList by sport and name and display the sorted list. Finally, sort the array list by sport and ranking and display the sorted list again. Hints: Because you need to sort the Athletes in two different ways, you will probably need to both implement Comparable and make a new Comparator class. You may need to add getter methods for the fields in your Athlete class, and it might be helpful to override the toString method to make it easier to display the objects. Examples Unsorted John Doe baseball 3) Sam Johnson football 2 Kevin Smith (baseball 1) Sally Johnson (swimming 3) James Smith (swimming 4 Meagan Kelly (swimming 1) Sorted by sport and then name John Doe baseball 3) Kevin Smith (baseball 1) Sam Johnson football 2) James Smith (3wimming 4) Meagan Kelly (swimming 1) Sally Johnson Swimming 3) Sorted by sport and then ranking Kevin Smith (baseball 1 John Doe baseball 3) Sam Johnson football 2 Meagan Kelly (swimming 1) Sally Johnson (swimming 3) James Smith (swimming 4)

Explanation / Answer

This is coded in netbeans only, i have got the same output shown in the question. here is the code and find output at the end.

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

package cheggtest;

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

/**
*
* @author AnishNair
*/
class AthletesTest
{
String name;
String sportname;
int rank;

public String getName() {
return name;
}

public String getSportname() {
return sportname;
}

public AthletesTest(String name, String sportname, Integer rank) {
this.name = name;
this.sportname = sportname;
this.rank = rank;
}

public int getRank() {
return rank;
}

public void setName(String name) {
this.name = name;
}

public void setSportname(String sportname) {
this.sportname = sportname;
}

public void setRank(int rank) {
this.rank = rank;
}
  
}

class AthletesComparatorBySportAndName implements Comparator<AthletesTest> {

@Override
public int compare(AthletesTest o1, AthletesTest o2) {
int flag =o1.getSportname().compareTo(o2.getSportname());
if(flag==0) flag = o1.getName().compareTo(o2.getName());
return flag;
}

}

class AthletesComparatorBySportAndRank implements Comparator<AthletesTest> {

@Override
public int compare(AthletesTest o1, AthletesTest o2) {
int flag =o1.getSportname().compareTo(o2.getSportname());
if(flag==0) flag = o1.getRank()-o2.getRank();
return flag;
}

}
public class Athletes {
  
public static void main(String args[])
{
ArrayList<AthletesTest> arrlist = new ArrayList<AthletesTest>();
AthletesTest a1 = new AthletesTest("John Doe","baseball",3);
AthletesTest a2 = new AthletesTest("Sam Johnson","football",2);
AthletesTest a3 = new AthletesTest("Kevin Smith","baseball",1);
AthletesTest a4 = new AthletesTest("Sally Johnson","swimming",3);
AthletesTest a5 = new AthletesTest("James Smith","swimming",4);
AthletesTest a6 = new AthletesTest("Meagan Kelly","swimming",1);
arrlist.add(a1);
arrlist.add(a2);
arrlist.add(a3);
arrlist.add(a4);
arrlist.add(a5);
arrlist.add(a6);
System.out.println("Unsorted data");
for(AthletesTest b:arrlist){
System.out.println(b.getName()+" "+b.getSportname()+" "+b.getRank()+" ");
}
System.out.println("Sportname and name ");
Collections.sort(arrlist, new AthletesComparatorBySportAndName());
for(AthletesTest b:arrlist){
System.out.println(b.getName()+" "+b.getSportname()+" "+b.getRank()+" ");
}
System.out.println("Sportname and rank ");
Collections.sort(arrlist, new AthletesComparatorBySportAndRank());
for(AthletesTest b:arrlist){
System.out.println(b.getName()+" "+b.getSportname()+" "+b.getRank()+" ");
}
}
}
  

Output:

Unsorted data

John Doe baseball 3

Sam Johnson football 2

Kevin Smith baseball 1

Sally Johnson swimming 3

James Smith swimming 4

Meagan Kelly swimming 1

Sportname and name

John Doe baseball 3

Kevin Smith baseball 1

Sam Johnson football 2

James Smith swimming 4

Meagan Kelly swimming 1

Sally Johnson swimming 3

Sportname and rank

Kevin Smith baseball 1

John Doe baseball 3

Sam Johnson football 2

Meagan Kelly swimming 1

Sally Johnson swimming 3

James Smith swimming 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote