Arrays of Objects
A reminder of our classes so far
A reminder - we have the following classes shown the UML diagram shown to the right.
Person is the superclass and there are two subclasses - Player and Coach.
We now want to add a new Team class.
We have now added a Team class to the UML diagram.
Containing the following new properties
teamName and sport
squad (array of Player objects)
trainers (array of Coach objects)
Containing the following methods
addPlayer (adds a new Player object to the squad property)
addCoach (adds a new Player object to the squad property)
displayTeam (displays info about team, players, coaches)
Is important to note that the arrow has changed from the hollow white arrow.
This will be our way to show that there is a relationship but that it is not an inheritance relationship. The Team class does not inherit the properties and methods of the Player or Coach classes.
Implemented Team class
The addPlayer method adds a new instance of a Player object to the empty squad array.
The addCoach method adds a new instance of a Coach object to the empty squad trainers array.
Is important to note that the team class is using the sayHello method of the relevant objects - again a reminder of the concept of encapsulation. The Team class cannot access the properties of either the Player or Coach classes.
Testing the Team Class
The code below will test the methods of the new Team class.
Code Output
Team Info
Pathway U18 plays WBBL
Hi my name is Kerry my shirt number is 77
And am currently the captain
Hi my name is Zac my shirt number is 27
Hi, I am Erin and am a Level 3 coach
Hi, I am Lisa and am a Level 2 coach
Visualisation of the array of objects
When we are adding players to the team we are creating references to a player object and these are what the objects in the squad array are pointing to.
It is similar for the trainers array.