Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You need to create classes for the player and team with appropriate members.

Include two functions in class Team:
A function to add new player to the team
A function to calculate and return the total score of the team by adding the individual score of its players.

A team has team name, country of the team, maximum of 5 team players. A player has the data of player name and player individual score. The search function reads the team name and displays all the information of the team including the team players and their scores. The leader board function shows all the teams (name and total score) from the highest to the lowest score.

Now I have create class of team and player. but my add player have error of invalid method declaration; return type required and cannot find symbol method add(player)

anyone have help me to fixed it

What I have tried:

Java
public class Player {
    private String name;
    private double score;
    private Player player;
    
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public double getScore(){
        return score;
    }
    public void setscore(double score){
       this.score=score;
    }
    public Player(String name, double score){
        this.name=name;
        this.score=score;
    }
    public addPlayer(String name, double score){
        player.add(player);
    }
    @Override
    public String toString(){
    return "\nName: "+name+"\nScore:"+score;
    }
}



Java
public class Team {
    private String teamname;
    private String countryname;
    private Player player;
    private int teamscore;

    public String getteamName(){
    return teamname;
    }
    public void setteamName(String teamname){
    this.teamname = teamname;
    }
    public String getCountryname(){
    return countryname;
    }
    public void setCountry(String countryname){
    this.countryname = countryname;
    }
    public Player getPlayer(){
    return player;
    }
    public void setPlayer(Player player){
    this.player=player;
    }
    public void addPlayer(Player player){
    player.add(player);
    }
    public void teamScore(int score){
    teamscore+=score;
    }

    @Override
    public String toString() {
    return "Team{" + "teamname=" + teamname + ", countryname=" + countryname + ", player=" + player + ", teamscore=" + teamscore + '}';
    }
}
Posted
Updated 27-Oct-20 1:14am
v3
Comments
Maciej Los 27-Oct-20 4:42am    
Take a look at the Team class. You've got only one player, but you have to be able to add more than one player (max. 5). I'd suggest to use ArrayList:
private ArrayList<Player) players = new ArrayList<>();
if(players.size<5)
  players.add(player);

The class constructor should take the name and country, rather than using a setter for each property. Creating a Team without a name is fairly pointless. Also class mambers do not need the prefix "team", as you already know that they are Team properties. So your class should be something like:
Java
public class Team {
    private String name;
    private String country;
    private int score;
    private Player player; // this should be an array or list

    public Team(String name, String country) { // the constructor needs the name and country
        this.name = name;
        this.country = country;
        score = 0;
    }

    public String toString(){ // a better mechanism to get the team name
        return name; // or possibly name + country in some form
    }
}
 
Share this answer
 
Comments
Maciej Los 27-Oct-20 7:14am    
5ed!
Richard MacCutchan 27-Oct-20 7:36am    
Thanks Maciej.
As i mentioned in the comment to the question...

Take a look at the definition of Team class. You've got only one player, but you have to be able to add more than one player (max. 5). I'd suggest to use ArrayList[^]:

Java
private ArrayList<Player> players = new ArrayList<>();
//then - in "add" method:
if(players.size<5)
  players.add(player);


Please, read Richard's answer also.
 
Share this answer
 
v2
Comments
Richard MacCutchan 27-Oct-20 7:38am    
+5, but there is a typo in the ArrayList declaration.
Maciej Los 27-Oct-20 7:50am    
This typo was unintended ;)
[EDIT]
Another typo, arghhh....
Look at your code:
C#
public addPlayer(String name, double score){
        player.add(player);
That is the syntax for a class constructor, not a method within the class. All methods (other than constructors) must include a return type:
C#
public void addPlayer(String name, double score){
        player.add(player);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900