Peter Holdensgaard
Peter Holdensgaard

Reputation: 57

Connecting two classes java object-oriented

I wish to make two new classes, where I can connect these. I want to create a player and team class. My goals is to assign a player to a team. Basically I wish to use a class within a class as attribute.

I imagine my player class be like;

public class Player {
    String first_name;
    String last_name;
    int age;
}

and my team to be something like;

public class Team {
    String Member1;
    String Member2;
}

and my main class to include;

Player Player1 = new Player("Peter", "Jensen", 24);

So what I want to do is calculate the average age of my players in a specific team. Any ideas or tips?

Upvotes: 0

Views: 4656

Answers (5)

Peter Holdensgaard
Peter Holdensgaard

Reputation: 57

My solution

public class Team {
List<Player> allPlayer;

// constructor, getter and setters
}

and

public class Player {
String name;
// constructor, getter and setters
}

Thanks for the help!

Upvotes: -2

fxrbfg
fxrbfg

Reputation: 1786

In real life what is a team? it's a bunch of players so you need to make Team consist of Players, after that ask a question "where is a biggest amount of information to fulfill computation of average players age?" This information placed inside of the Team class (players list) and we need to place method that calculate average age to Team class. It's called information expert principle, one of the many good oop design principles.

https://en.wikipedia.org/wiki/GRASP_(object-oriented_design)#Information_expert

So in this way code will be like that:

Player class:

  public class Player {
        private String firstName;
        private String lastName;
        private int age; 

        public Player(String firstName, String lastName, int age) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }

        public String firstName() {
            return firstName;
        }

        public String lastName() {
            return lastName;
        }

        public String age() {
            return age;
        }

    }

Team class:

  public class Team {
        private List<Player> players;

        Team(List<Player> players) { 
           this.players = players;
        }

        public void add(Player player) {
            players.add(player);
        }

        public void remove(Player player) {
            players.remove(player);
        }

        public double averageAge() {
            double sum = 0;
            double amount = players.size();
            for (Player player : players) {
                sum += player.age();
            }
            return sum / amount;
        }

    }

after that we able to use this classes in every convenient way, example:

public class Test {

    public static void main(String[] args) {
        Team team = new Team(Arrays.asList(new Player[] {
            new Player("Peter", "Jensen", 24),
            new Player("Foo", "Bar", 50)
        }));
        System.out.println(team.averageAge()); // 37
    }
}

Simple ask to team what is average players age, and team will respond to you.

Upvotes: 5

lewis
lewis

Reputation: 23

In your team you need to store a collection of players:

public class Team{
    private List<Player> players;

    public Team() {
        players = new ArrayList<?>();
    }

    public void addPlayer(Player player) {
        players.add(player);
    }

    public Int getAverageAge() {
        Int count =0;
        Int sumAge=0;
        for(Player player:players) {
            sumAge+=player.getAge();//this getter should be in Player class
            count++;
        }
        return sumAge/count;
    }
}

Use the addPlayer() method to add your player to the list in main class

Upvotes: 1

Ousmane D.
Ousmane D.

Reputation: 56489

create a List or an array of players (if there is a fixed number of players allowed) inside the Team class to represent the team of players.

Example:

class Team {
   private List<Player> players = new ArrayList<>();

   public void setPlayers(List<Player> players){
          this.players = players;
   }

   public List<Player> getPlayers(){
       return Collections.unmodifiableList(players);
   }
}

then create your List of objects:

Team team = new Team();
team.setPlayers(new ArrayList<>(Arrays.asList( new Player("Peter", "Jensen", 24),
                new Player("Danny", "peters", 19),
                new Player("Michael", "Jones", 32))));

then you can calculate the average age like so:

double averageAge = team.getPlayers()
                        .stream()
                        .mapToDouble(Player::getAge)
                        .average().orElse(0); // 0 if no player present in the list

note - the example above uses a list but you could as easily just use an array, it also uses the streams API and if you're not familiar with it then you can do it like this:

double averageAge = 0; // default if no player is present
List<Player> players = team.getPlayers();
for (Player player : players) {
     averageAge += player.getAge();
}

if(players.size() >= 1) {
    averageAge /= players.size();
}

if for some reason you also want the ability to add a given player at a time then you can create a void addPlayer(Player player){...} method inside the Team class to take a given player and add it to the list of players.

Upvotes: 3

WLA
WLA

Reputation: 15

You can add getter and setter method to player class.Also if you want you can do something like this in player class:

public static Player newInstance(String n,String Sn,int age){
    Player a=new Player(n,Sn,age);
    return a;

}

Now you can easily add player to team.But members cant be String.My advice use ArrayList.You can find many articles about that.

And you can use 'for(Player a:ListName)'it will make your things easier.

Upvotes: 0

Related Questions