1

I have three classes called League, Team and Player, League has an array with teams, and I have a method called showLeagueTable that shows team sorted by ranking(which is a method that exists in the Team class) and the class Team has an array of players.

League.java

public class League<T extends Team> {
    public String name;
    private List<T> teams = new ArrayList<>();

    public League(String name) {
        this.name = name;
    }

    public boolean add(T team) {
        if (teams.contains(team)) {
            return false;
        }

        return teams.add(team);
    }

    public void showLeagueTable() {
        Collections.sort(this.teams);

        for (T t: teams) {
            System.out.println(t.getName() + ": " + t.ranking());
        }
    }
}

Team.java

public class Team<T extends Player> implements Comparable<Team<T>> {
     private String name;
     int played = 0;
     int won = 0;
     int lost = 0;
     int tied = 0;

     private ArrayList<T> members = new ArrayList<>();

    public Team(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean addPlayer(T player) {
        if (members.contains(player)) {
            System.out.println(player.getName() + " is already on this team");
            return false;
        } else {
            members.add(player);
            System.out.println(player.getName() + " picked for team " + this.name);

            return true;
        }
    }

    public int numPlayers() {
        return this.members.size();
    }

    public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
        String message;
        if (ourScore > theirScore) {
            won++;
            message = " beat ";
        } else if (ourScore == theirScore) {
            tied++;
            message = " drew with ";
        } else {
            lost++;
            message = " lost to ";
        }

        played++;

        if (opponent != null) {
            System.out.println(this.getName() + message + opponent.getName());
            opponent.matchResult(null, theirScore, ourScore);
        }
    }

    public int ranking() {
        return (won * 2) + tied;
    }

    @Override
    public int compareTo(Team<T> team) {
        return Integer.compare(team.ranking(), this.ranking());
    }
}

Player.java

public class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

but in the line Collections.sort(this.teams); from the class League, I got the warning Unchecked method 'sort(List<T>)' invocation, I have read that maybe the problems is that I haven't implemented the Comparable interface in my class Team with a Type, but I did it you can see it at the line:

public class Team<T extends Player> implements Comparable<Team<T>>

can someone help me, please, thanks!

Max Peng
  • 2,879
  • 1
  • 26
  • 43
Yoel Monzon
  • 111
  • 5

1 Answers1

-1

here a fully workable code for you specific issue :
it removes warning and errors.
phase 1 : remove comparable behaviour
phase 2 : try to compile without warning
phase 3 : try with a real new allocation : imporant, because, if you do not do anything new, T generics are not used, and you can not detect errors.
phase 4 : adding comparable behaviour.

final source [ see A) B) and C) mistakes in source]

League.java

package test;

import java.util.ArrayList;
import java.util.List;
import test.Team;
import java.util.Collections;

// -- A) missing precision about Team , Team based on Player 
// -- public class League<T extends Team> 
public class League<T extends Team<E>, E extends Player>
{
    public String name;
    
    
    // B) ! miss <T> after new : 
    //    private List<T> teams = new ArrayList<>();
    private List<T> teams = new ArrayList<T>();
    
    

    public League(String name) {
        this.name = name;
    }

    public boolean add(T team) {
        if (teams.contains(team)) {
            return false;
        }

        return teams.add(team);
    }

    public void showLeagueTable() {
        Collections.sort(this.teams);

        for (T t: teams) {
            System.out.println(t.getName() + ": " + t.ranking());
        }
    }
}

Team.java

    package test;
    
    
    import java.util.ArrayList;
    
       
    public class Team<T extends Player> implements Comparable<Team<T>> {
           
        private String name;
        int played = 0;
        int won = 0;
        int lost = 0;
        int tied = 0;
    
        // C) !! MISS <T> after new ArrayList
        // private ArrayList<T> members = new ArrayList<>();
        private ArrayList<T> members = new ArrayList<T>();
    
    
        @Override
        public int compareTo(Team<T> team) {
            return Integer.compare(team.ranking(), this.ranking());
        }
        
        
        
       public Team(String name) {
           this.name = name;
       }
    
       public String getName() {
           return name;
       }
    
       public boolean addPlayer(T player) {
           if (members.contains(player)) {
               System.out.println(player.getName() + " is already on this team");
               return false;
           } else {
               members.add(player);
               System.out.println(player.getName() + " picked for team " + this.name);
    
               return true;
           }
       }
    
       public int numPlayers() {
           return this.members.size();
       }
       
       public void showPlayers() {
           for (T element : members) {
               System.out.println(element.getName());
           }
       }
    
       public void matchResult(Team<T> opponent, int ourScore, int theirScore) {
           String message;
           if (ourScore > theirScore) {
               won++;
               message = " beat ";
           } else if (ourScore == theirScore) {
               tied++;
               message = " drew with ";
           } else {
               lost++;
               message = " lost to ";
           }
    
           played++;
    
           if (opponent != null) {
               System.out.println(this.getName() + message + opponent.getName());
               opponent.matchResult(null, theirScore, ourScore);
           }
       }
    
       public int ranking() {
           return (won * 2) + tied;
       }
    
    }

Player.java

package test;

public class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Test.java

package test;

import test.League;
import test.Team;
import test.Player;

public class Test {

     Player p1;
     Player p2;
     Player p3;
     Player p4;
     Player p5;
     Player p6;
     Player p7;
     Player p8;
     Player p9;
        
     Team<Player> t1;
     Team<Player> t2;
     Team<Player> t3;
     Team<Player> t4;

    
    public void test() {
        
        System.out.println("RUNNING TEST");
        
         p1=new Player("Mike");
         p2=new Player("John");
         p3=new Player("Jenny");
         p4=new Player("Sunny");
         p5=new Player("Mike");
         p6=new Player("Jeremy");
         p7=new Player("Gwen");
         p8=new Player("Hector");
         p9=new Player("Henry");
            
         t1=new Team<Player>("Team1");
         t2=new Team<Player>("Team2");
         t3=new Team<Player>("Team3");
         t4=new Team<Player>("Team4");
         
         t1.addPlayer(p1);
         t1.addPlayer(p2);
         t2.addPlayer(p3);
         t2.addPlayer(p4);
         t2.addPlayer(p5);
         t3.addPlayer(p6);
         t3.addPlayer(p7);
         t4.addPlayer(p8);
         t4.addPlayer(p9);
         
    
         // test show players
         t1.showPlayers();
         System.out.println("------------");
         System.out.println("num players in team is "+t1.numPlayers());
        
         System.out.println("---adding team in league ---------");
         League<Team<Player>, Player> g1=new League<Team<Player>, Player>("League 1");
         g1.add(t1);
         g1.add(t2);
         
         League<Team<Player>, Player> g2=new League<Team<Player>, Player>("League 2");
         g2.add(t3);
         g2.add(t4);
         
         System.out.println("---settings results ---------");
         t1.matchResult(t2, 2, 8);
         t2.matchResult(t1, 1, 7);
         t3.matchResult(t4, 4, 5);
         t3.matchResult(t4, 4, 0);
         
         System.out.println("-----League 1---------");
         g1.showLeagueTable();
         System.out.println("-----League 2---------");
         g2.showLeagueTable();
         
     
         
         
    }
    
}

Main.java

import test.Test;

public class Main  
{
   
    public static void main(String[] args) {        
      System.out.println("running MAIn");
      Test test = new Test();
      test.test();
    }    
}
Community
  • 1
  • 1
Eric
  • 608
  • 4
  • 11
  • Why do you add generic types to the diamond operator? Those are superfluous. Also repeating a lot of unchanged code just bloats this answer and this, this question is a duplicate and doesn't need an additional answer in the first place. – Tom Nov 06 '19 at 14:19
  • thanks the `A` comment helped me, the B and C are not necessary – Yoel Monzon Nov 06 '19 at 16:58