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

need help writing this program in java. directions: Your task is to develop an A

ID: 3823408 • Letter: N

Question

need help writing this program in java.

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 sport and 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.

output example:

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 (swimming - 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

There are 3 classes required for fully implementing this.

Athlete.java

NameComparator.java

TestClass.java

//output generated for this code

Sorted by sport and then 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)
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)

//Athlete.java


public class Athlete<T extends Comparable<T>> implements Comparable<Athlete<T>> {

   private String name;
   private String sport;
   private T ranking;
  
   public Athlete(String name, String sport, T ranking) {
       super();
       this.name = name;
       this.sport = sport;
       this.ranking = ranking;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getSport() {
       return sport;
   }
   public void setSport(String sport) {
       this.sport = sport;
   }
   public T getRanking() {
       return ranking;
   }
   public void setRanking(T ranking) {
       this.ranking = ranking;
   }

   public int compareTo(Athlete<T> obj) {
           int ret = 0;
           ret = sport.compareTo(obj.getSport());
           if(ret == 0)
           {
               ret = name.compareTo(obj.getName());
              
           }
           return ret;
   }
  
   @Override
   public String toString() {
      
       return name + " (" + sport + " - " + ranking.toString()+ ")";
   }
  

}

//NameComparator.java

import java.util.Comparator;


public class NameComparator<T extends Comparable<T>> implements Comparator<Athlete<T>> {

   public int compare(Athlete<T> o1, Athlete<T> o2) {
       int ret =0;
       ret = o1.getSport().compareTo(o2.getSport());
       if(ret == 0)
       {
           ret = o1.getRanking().compareTo(o2.getRanking());
       }
       return ret;
   }

}

//TestClass.java

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


public class TestClass {

   public static void main(String[] args) {
       ArrayList<Athlete<Integer>> array= new ArrayList<Athlete<Integer>>();
       array.add(new Athlete<Integer>("John Doe", "baseball", 3));
       array.add(new Athlete<Integer>("Sam Johnson", "football", 2));
       array.add(new Athlete<Integer>("Kevin Smith", "baseball", 1));
       array.add(new Athlete<Integer>("Sally Johnson", "swimming", 3));
       array.add(new Athlete<Integer>("James Smith", "swimming", 4));
       array.add(new Athlete<Integer>("Meagan Kelly", "swimming", 1));
      
       Collections.sort(array);
       System.out.println("Sorted by sport and then name");
       for(Athlete<Integer> athlete:array)
       {
           System.out.println(athlete.toString());
       }
       System.out.println("Sorted by sport and then ranking");
       Collections.sort(array, new NameComparator<Integer>());
      
       for(Athlete<Integer> athlete:array)
       {
           System.out.println(athlete.toString());
       }
   }

}