Create a class called Team, which represents a team of BaseballPlayers (Copy and
ID: 3903019 • Letter: C
Question
Create a class called Team, which represents a team of BaseballPlayers (Copy and paste your BaseballPlayer class from Assignment 1).
This is the Baseball player class:
{
// Fields
private BaseballPlayer[] players;
private String teamName;
public Team(BaseballPlayer[] p, String t)
// Returns the amount of players on the team
public int rosterSize()
// Returns a random player from the roster
public BaseballPlayer randomPlayer()
// Returns an array of 9 random players from the roster
public BaseballPlayer[] randomFieldPlayers()
{
BaseballPlayer[] random = new BaseballPlayer[9];
//Add code here
}
// Returns a string containing the team name and the roster size
// Example: The Yankees have 15 players on their roster.
public String toString()
// Include getters and setters for all the fields
}
Write in Java and at a simple level
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Team.java
=======
import java.util.Random;
public class Team
{
// Fields
private BaseballPlayer[] players;
private String teamName;
public Team(BaseballPlayer[] p, String t)
{
players = p;
teamName = t;
}
// Returns the amount of players on the team
public int rosterSize()
{
return players.length;
}
// Returns a random player from the roster
public BaseballPlayer randomPlayer()
{
Random rand = new Random();
int index = rand.nextInt(players.length);
return players[index];
}
// Returns an array of 9 random players from the roster
public BaseballPlayer[] randomFieldPlayers()
{
BaseballPlayer[] random = new BaseballPlayer[9];
//Add code here
Random r = new Random();
for(int i = 0; i < random.length;)
{
int index = r.nextInt(players.length);
boolean dup = false;
//make sure we have not already added the player
for(int j = 0; j < i; j++)
{
if(random[j].getJerseyNumber() == players[index].getJerseyNumber())
{
dup = true;
break;
}
}
if(!dup)
random[i++] = players[index];
}
return random;
}
// Returns a string containing the team name and the roster size
// Example: The Yankees have 15 players on their roster.
public String toString()
{
return "The " + teamName + " have " + rosterSize() + " players on their roster";
}
// Include getters and setters for all the fields
public BaseballPlayer[] getPlayers() {
return players;
}
public void setPlayers(BaseballPlayer[] players) {
this.players = players;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.