Question: A team has players. Use aggregation to complete the class constructor and the listTeam and addPlayer methods of the Team class below. Output should be
A team has players. Use aggregation to complete the class constructor and the listTeam and addPlayer methods of the Team class below.
Output should be formatted as follows:
[Player1-Role1, Player2-Role2, Player3-Role3, Player4-Role4, Player5-Role5, Player6-Role6]
Complete the following code:
/** Represents a sports team */ public class Team { public Team() { // TODO: Use aggregation to create a // team roster consisting of 6 players. } /** Produces a listing of all team members and their positions. @return a formatted list of all team members and their positions */ public String listTeam() { // TODO: Complete this method to provide a string value containing the // complete list of players, formatted as shown in the instructions. } /** Adds a new player to the team roster. * @param aPlayer a Player object */ public void addPlayer(Player aPlayer) { // TODO: Complete this method to add a player to the team. } } The following classes are used to check your work:
public class TeamTester { public static void main(String[] args) { Team aTeam = new Team(); aTeam.addPlayer(new Player("Fred","Left wing")); aTeam.addPlayer(new Player("Carl","Right wing")); aTeam.addPlayer(new Player("Louie","Center")); aTeam.addPlayer(new Player("Max","Defense")); aTeam.addPlayer(new Player("Jason","Defense")); aTeam.addPlayer(new Player("Alphonse","Goalie")); aTeam.addPlayer(new Player("Juliet","Coach")); // shouldn't be added System.out.println(aTeam.listTeam()); System.out.println("Expected: [Fred-Left wing, Carl-Right wing, " + "Louie-Center, Max-Defense, Jason-Defense, Alphonse-Goalie]"); } } public class Player { private String name; private String position; public Player (String aName, String aPosition) { name = aName; position = aPosition; } public String getName(){ return name; } public String getPosition(){ return position; } public String toString(){ return name + "-" + position; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
