SlideShare a Scribd company logo
Write a class (BasketballTeam) encapsulating the concept of a team of basketball players. This
method should have a single instance variable which is an array of basketball player objects. In
addition to that class, you will need to design and a code a Playerclass to encapsulate the concept
of a basketball player, assuming a basketball player has the following attributes: a name, a
position, number of shots taken, and number of shots made.
In your Player class you should write the following methods:
A constructor taking a String for the name, a String for the position, an int for the number of
shots taken, and an int for the number of shots made.
Accessor, mutator, toString, and equals methods.
A method returning the individual shooting percentage. Avg = Shots Made / Shots Taken. Hint:
This method should return a double!!!
In your BasketballTeam class you should write the following methods:
A constructor taking an array of Player objects as its only parameter and assigning that array to
the array data member of the class, its only instance variable.
Accessor, mutator, toString, and equals methods.
A method checking if all positions are different, returning true if they are, false if they are not.
A method returning the shooting percentage of the team. Team Shooting Percentage = Total
Shots Made / Total Shots Taken Hint: This should return a double!!! Also, this is not the average
of the the averages.
A method checking that we have a center (that is, the name of the position) on the team. If we
do not have any return false; otherwise, it returns true;
A method returning void that sorts the array of Player objects in ascending order using the
number of shots taken as the sorting key.
A method returning the array of Player objects sorted in ascending alphabetical order by name.
Hint: Use compareTo method of the String class.
A method returning the name of the player with the most shots made.
A method returning the name of the player with the highest shooting average.
In your BasketballTeamClient class, when you test all your methods, you can hard-code five
basketball Player objects. This client should call all 12 methods including the constructor for full
credit.
When writing the Player class you should write a PlayerClient class that tests all its methods
before integrating into BasketballTeam class.
Solution
public class Player {
//Attributes
private String name;
private String position;
private int shotsTaken;
private int shotsMade;
/**
* Constructor
* @param name
* @param position
* @param shotsTaken
* @param shotsMade
*/
public Player(String name, String position, int shotsTaken, int shotsMade) {
this.name = name;
this.position = position;
this.shotsTaken = shotsTaken;
this.shotsMade = shotsMade;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the position
*/
public String getPosition() {
return position;
}
/**
* @param position the position to set
*/
public void setPosition(String position) {
this.position = position;
}
/**
* @return the shotsTaken
*/
public int getShotsTaken() {
return shotsTaken;
}
/**
* @param shotsTaken the shotsTaken to set
*/
public void setShotsTaken(int shotsTaken) {
this.shotsTaken = shotsTaken;
}
/**
* @return the shotsMade
*/
public int getShotsMade() {
return shotsMade;
}
/**
* @param shotsMade the shotsMade to set
*/
public void setShotsMade(int shotsMade) {
this.shotsMade = shotsMade;
}
@Override
public String toString() {
return "Player name: " + getName() + "tPosition: " + getPosition() +
" Shots Taken: " + getShotsTaken() + "ttShots Made: " + getShotsMade();
}
@Override
public boolean equals(Object obj) {
if(obj != null) {
if(obj instanceof Player) {
Player another = (Player)obj;
if((this.getName().equalsIgnoreCase(another.getName())) &&
(this.getPosition().equalsIgnoreCase(another.getPosition())) &&
(this.getShotsMade() == another.getShotsMade()) &&
(this.getShotsTaken() == another.getShotsTaken()))
return true;
}
}
return false;
}
/**
* Returns the individual shooting percentage
* @return
*/
public double shootingPercentage() {
return ((double)getShotsMade() / getShotsTaken());
}
}
/**
* This class tests the methods of the Player class
*
* @author
*
*/
public class PlayerClient {
public static void main(String[] args) {
// Create a Player object
Player player1 = new Player("John", "Center", 10, 4);
// Create another Player object
Player player2 = new Player("Bill", "Small forward", 10, 3);
// Test toString and shootingPercentage method
System.out.println("Player 1: " + player1);
System.out.println("Shooting percentage: " + player1.shootingPercentage());
System.out.println("Player 2: " + player2);
System.out.println("Shooting percentage: " + player2.shootingPercentage());
// Test equals method
if(player1.equals(player2))
System.out.println("Both the players are same");
else
System.out.println("Both the players are different");
}
}
SAMPLE OUTPUT:
Player 1: Player name: John Position: Center
Shots Taken: 10 Shots Made: 4
Shooting percentage: 0.4
Player 2: Player name: Bill Position: Small forward
Shots Taken: 10 Shots Made: 3
Shooting percentage: 0.3
Both the players are different
public class BasketballTeam {
// Attributes
private Player[] players;
/**
* Constructors
*
* @param players
*/
public BasketballTeam(Player[] players) {
this.players = players;
}
/**
* @return the players
*/
public Player[] getPlayers() {
return players;
}
/**
* @param players
* the players to set
*/
public void setPlayers(Player[] players) {
this.players = players;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (Player player : players) {
sb.append(player + " ");
}
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj != null) {
if (obj instanceof BasketballTeam) {
BasketballTeam another = (BasketballTeam) obj;
if (this.getPlayers().length == another.getPlayers().length) {
for (int i = 0; i < this.getPlayers().length; i++) {
if (!this.players[i].equals(another.players[i]))
break;
}
return true;
}
}
}
return false;
}
/**
* Checks if all positions are different, returns true if they are, false if
* they are not.
*
* @return
*/
public boolean isDifferentPosition() {
for (int i = 0; i < this.getPlayers().length - 1; i++) {
for (int j = i + 1; j < this.getPlayers().length; j++) {
if (this.players[i].getPosition().equals(this.players[j].getPosition()))
return false;
}
}
return true;
}
/**
* Returns the team's shooting percentage
*
* @return
*/
public double shootingPercentage() {
int teamShotsMade = 0;
int teamShotsTaken = 0;
for (Player player : players) {
teamShotsMade += player.getShotsMade();
teamShotsTaken += player.getShotsTaken();
}
return ((double) teamShotsMade / teamShotsTaken);
}
/**
* Checks whether the team has a center (that is, the name of the position)
* on the team. If yes returns true; otherwise, it returns false
*
* @return
*/
public boolean isCenterPresent() {
for (Player player : players) {
if (player.getPosition().equalsIgnoreCase("Center"))
return true;
}
return false;
}
/**
* Swaps 2 players
* @param p1
* @param p2
*/
private void swap(Player p1, Player p2) {
Player temp = new Player(p1.getName(), p1.getPosition(), p1.getShotsTaken(),
p1.getShotsMade());
p1.setName(p2.getName());
p1.setPosition(p2.getPosition());
p1.setShotsMade(p2.getShotsMade());
p1.setShotsTaken(p2.getShotsTaken());
p2.setName(temp.getName());
p2.setPosition(temp.getPosition());
p2.setShotsMade(temp.getShotsMade());
p2.setShotsTaken(temp.getShotsTaken());
}
/**
* Sorts the array of Player objects in ascending order using the number of
* shots taken as the sorting key.
*/
public void sortByShotsTaken() {
for (int i = 0; i < this.players.length - 1; i++) {
for (int j = i + 1; j < this.players.length; j++) {
if (this.players[i].getShotsTaken() > this.players[j].getShotsTaken()) {
swap(this.players[i], this.players[j]);
}
}
}
}
/**
* Returns the array of Player objects sorted in ascending alphabetical order by name.
* @return
*/
public Player[] sortByName() {
// Create copy os this array
Player[] sortedArr = new Player[this.players.length];
for (int i = 0; i < sortedArr.length; i++) {
sortedArr[i] = new Player(this.players[i].getName(), this.players[i].getPosition(),
this.players[i].getShotsTaken(), this.players[i].getShotsMade());
}
for (int i = 0; i < sortedArr.length - 1; i++) {
for (int j = i + 1; j < sortedArr.length; j++) {
if (sortedArr[i].getName().compareTo(sortedArr[j].getName()) > 0) {
swap(sortedArr[i], sortedArr[j]);
}
}
}
return sortedArr;
}
/**
* Returns the name of the player with the most shots made.
*/
public String playerWithMostShotsMade() {
int index = 0;
int max = this.players[0].getShotsMade();
for (int i = 1; i < this.players.length - 1; i++) {
if(max < this.players[i].getShotsMade()) {
max = this.players[i].getShotsMade();
index = i;
}
}
return this.players[index].getName();
}
/**
* Returns the name of the player with the highest shooting average
*/
public String playerWithHighestShootingAvg() {
int index = 0;
double max = this.players[0].shootingPercentage();
for (int i = 1; i < this.players.length; i++) {
double avg = this.players[i].shootingPercentage();
if(max < avg) {
max = avg;
index = i;
}
}
return this.players[index].getName();
}
}
/**
* This class tests the methods of BasketballTeam class
*
* @author
*
*/
public class BasketballTeamClient {
public static void main(String[] args) {
// Create Player array with 5 objects
Player[] players = new Player[5];
players[0] = new Player("Bill", "Center", 10, 2);
players[1] = new Player("Joe", "Point guard", 10, 7);
players[2] = new Player("Andrew", "Shooting guard", 20, 2);
players[3] = new Player("James", "Power forward", 20, 12);
players[4] = new Player("Ben", "Small forward", 10, 4);
// Test the constructor
BasketballTeam team = new BasketballTeam(players);
// Display team
System.out.println("Team: " + team);
System.out.println();
// Check if all positions are different
System.out.println("Are all positions different? " + (team.isDifferentPosition() ? "Yes" :
"No"));
System.out.println();
// Team Shooting Percentage
System.out.println("Team Shooting Percentage: " + team.shootingPercentage());
System.out.println();
// Check if center position is present
System.out.println("Is center player available: " + (team.isCenterPresent() ? "Yes" :
"No"));
System.out.println();
// Sort array using the number of shots taken
team.sortByShotsTaken();
System.out.println("Sorted team using the number of shots taken as the sorting key: " +
team);
System.out.println();
// Sort team in ascending alphabetical order by name.
Player[] sortedArr = team.sortByName();
System.out.println("Sorted team in ascending alphabetical order by name: " );
for (Player player : sortedArr) {
System.out.println(player);
}
System.out.println();
// Player with the most shots made.
System.out.println("Player with the most shots made: " +
team.playerWithMostShotsMade());
System.out.println();
// Player with the highest shooting average.
System.out.println("Player with the highest shooting average: " +
team.playerWithHighestShootingAvg());
}
}
SAMPLE OUTPUT:
Team:
Player name: Bill Position: Center
Shots Taken: 10 Shots Made: 2
Player name: Joe Position: Point guard
Shots Taken: 10 Shots Made: 7
Player name: Andrew Position: Shooting guard
Shots Taken: 20 Shots Made: 2
Player name: James Position: Power forward
Shots Taken: 20 Shots Made: 12
Player name: Ben Position: Small forward
Shots Taken: 10 Shots Made: 4
Are all positions different? Yes
Team Shooting Percentage: 0.38571428571428573
Is center player available: Yes
Sorted team using the number of shots taken as the sorting key:
Player name: Bill Position: Center
Shots Taken: 10 Shots Made: 2
Player name: Joe Position: Point guard
Shots Taken: 10 Shots Made: 7
Player name: Ben Position: Small forward
Shots Taken: 10 Shots Made: 4
Player name: James Position: Power forward
Shots Taken: 20 Shots Made: 12
Player name: Andrew Position: Shooting guard
Shots Taken: 20 Shots Made: 2
Sorted team in ascending alphabetical order by name:
Player name: Andrew Position: Shooting guard
Shots Taken: 20 Shots Made: 2
Player name: Ben Position: Small forward
Shots Taken: 10 Shots Made: 4
Player name: Bill Position: Center
Shots Taken: 10 Shots Made: 2
Player name: James Position: Power forward
Shots Taken: 20 Shots Made: 12
Player name: Joe Position: Point guard
Shots Taken: 10 Shots Made: 7
Player with the most shots made: James
Player with the highest shooting average: Joe

More Related Content

More from rozakashif85 (20)

PDF
Give background information concerning EMT and include what is known.pdf
rozakashif85
 
PDF
Explain how the current economic recession differs from the depressio.pdf
rozakashif85
 
PDF
Does yeast uses CopI and CopII formation for vesicle budding to grow.pdf
rozakashif85
 
PDF
DNA molecules consist of chemically linked sequences of the bases ad.pdf
rozakashif85
 
PDF
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
rozakashif85
 
PDF
Count Red Nodes. Write a program that computes the percentage of red.pdf
rozakashif85
 
PDF
apple 2008 historically what were Apples major competitive advanta.pdf
rozakashif85
 
PDF
A partial results from a PERT analysis for a project with 50 activit.pdf
rozakashif85
 
PDF
briefly write about the variability of natural systems and the signi.pdf
rozakashif85
 
PDF
A graph is symmetric with respect to the vertical line corresponding.pdf
rozakashif85
 
PDF
X-Linked Recessive Write three rules to keep in mind when counseling .pdf
rozakashif85
 
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
PDF
Which of the following would you expect to happen in a plant that doe.pdf
rozakashif85
 
PDF
What is the value of including personal information about yourself a.pdf
rozakashif85
 
PDF
What is a voluntary response sampleSolutionVoluntary response.pdf
rozakashif85
 
PDF
What adaptive benefit do these abilities give wolbachia In oth.pdf
rozakashif85
 
PDF
Think about autumn (Fall season) in places like New England (Pennsyl.pdf
rozakashif85
 
PDF
The orange is which type of fruit Simple - Legume Simple - Drupe .pdf
rozakashif85
 
PDF
The _________ is the protective chamber that houses the ovule and Lat.pdf
rozakashif85
 
PDF
Suppose X follows a normal distribution with mean=1 and variance=4. .pdf
rozakashif85
 
Give background information concerning EMT and include what is known.pdf
rozakashif85
 
Explain how the current economic recession differs from the depressio.pdf
rozakashif85
 
Does yeast uses CopI and CopII formation for vesicle budding to grow.pdf
rozakashif85
 
DNA molecules consist of chemically linked sequences of the bases ad.pdf
rozakashif85
 
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
rozakashif85
 
Count Red Nodes. Write a program that computes the percentage of red.pdf
rozakashif85
 
apple 2008 historically what were Apples major competitive advanta.pdf
rozakashif85
 
A partial results from a PERT analysis for a project with 50 activit.pdf
rozakashif85
 
briefly write about the variability of natural systems and the signi.pdf
rozakashif85
 
A graph is symmetric with respect to the vertical line corresponding.pdf
rozakashif85
 
X-Linked Recessive Write three rules to keep in mind when counseling .pdf
rozakashif85
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
Which of the following would you expect to happen in a plant that doe.pdf
rozakashif85
 
What is the value of including personal information about yourself a.pdf
rozakashif85
 
What is a voluntary response sampleSolutionVoluntary response.pdf
rozakashif85
 
What adaptive benefit do these abilities give wolbachia In oth.pdf
rozakashif85
 
Think about autumn (Fall season) in places like New England (Pennsyl.pdf
rozakashif85
 
The orange is which type of fruit Simple - Legume Simple - Drupe .pdf
rozakashif85
 
The _________ is the protective chamber that houses the ovule and Lat.pdf
rozakashif85
 
Suppose X follows a normal distribution with mean=1 and variance=4. .pdf
rozakashif85
 

Recently uploaded (20)

PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Ad

Write a class (BasketballTeam) encapsulating the concept of a tea.pdf

  • 1. Write a class (BasketballTeam) encapsulating the concept of a team of basketball players. This method should have a single instance variable which is an array of basketball player objects. In addition to that class, you will need to design and a code a Playerclass to encapsulate the concept of a basketball player, assuming a basketball player has the following attributes: a name, a position, number of shots taken, and number of shots made. In your Player class you should write the following methods: A constructor taking a String for the name, a String for the position, an int for the number of shots taken, and an int for the number of shots made. Accessor, mutator, toString, and equals methods. A method returning the individual shooting percentage. Avg = Shots Made / Shots Taken. Hint: This method should return a double!!! In your BasketballTeam class you should write the following methods: A constructor taking an array of Player objects as its only parameter and assigning that array to the array data member of the class, its only instance variable. Accessor, mutator, toString, and equals methods. A method checking if all positions are different, returning true if they are, false if they are not. A method returning the shooting percentage of the team. Team Shooting Percentage = Total Shots Made / Total Shots Taken Hint: This should return a double!!! Also, this is not the average of the the averages. A method checking that we have a center (that is, the name of the position) on the team. If we do not have any return false; otherwise, it returns true; A method returning void that sorts the array of Player objects in ascending order using the number of shots taken as the sorting key. A method returning the array of Player objects sorted in ascending alphabetical order by name. Hint: Use compareTo method of the String class. A method returning the name of the player with the most shots made. A method returning the name of the player with the highest shooting average. In your BasketballTeamClient class, when you test all your methods, you can hard-code five basketball Player objects. This client should call all 12 methods including the constructor for full credit. When writing the Player class you should write a PlayerClient class that tests all its methods before integrating into BasketballTeam class. Solution
  • 2. public class Player { //Attributes private String name; private String position; private int shotsTaken; private int shotsMade; /** * Constructor * @param name * @param position * @param shotsTaken * @param shotsMade */ public Player(String name, String position, int shotsTaken, int shotsMade) { this.name = name; this.position = position; this.shotsTaken = shotsTaken; this.shotsMade = shotsMade; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the position */
  • 3. public String getPosition() { return position; } /** * @param position the position to set */ public void setPosition(String position) { this.position = position; } /** * @return the shotsTaken */ public int getShotsTaken() { return shotsTaken; } /** * @param shotsTaken the shotsTaken to set */ public void setShotsTaken(int shotsTaken) { this.shotsTaken = shotsTaken; } /** * @return the shotsMade */ public int getShotsMade() { return shotsMade; } /** * @param shotsMade the shotsMade to set */ public void setShotsMade(int shotsMade) { this.shotsMade = shotsMade; } @Override public String toString() { return "Player name: " + getName() + "tPosition: " + getPosition() +
  • 4. " Shots Taken: " + getShotsTaken() + "ttShots Made: " + getShotsMade(); } @Override public boolean equals(Object obj) { if(obj != null) { if(obj instanceof Player) { Player another = (Player)obj; if((this.getName().equalsIgnoreCase(another.getName())) && (this.getPosition().equalsIgnoreCase(another.getPosition())) && (this.getShotsMade() == another.getShotsMade()) && (this.getShotsTaken() == another.getShotsTaken())) return true; } } return false; } /** * Returns the individual shooting percentage * @return */ public double shootingPercentage() { return ((double)getShotsMade() / getShotsTaken()); } } /** * This class tests the methods of the Player class * * @author * */ public class PlayerClient { public static void main(String[] args) { // Create a Player object Player player1 = new Player("John", "Center", 10, 4);
  • 5. // Create another Player object Player player2 = new Player("Bill", "Small forward", 10, 3); // Test toString and shootingPercentage method System.out.println("Player 1: " + player1); System.out.println("Shooting percentage: " + player1.shootingPercentage()); System.out.println("Player 2: " + player2); System.out.println("Shooting percentage: " + player2.shootingPercentage()); // Test equals method if(player1.equals(player2)) System.out.println("Both the players are same"); else System.out.println("Both the players are different"); } } SAMPLE OUTPUT: Player 1: Player name: John Position: Center Shots Taken: 10 Shots Made: 4 Shooting percentage: 0.4 Player 2: Player name: Bill Position: Small forward Shots Taken: 10 Shots Made: 3 Shooting percentage: 0.3 Both the players are different public class BasketballTeam { // Attributes private Player[] players; /** * Constructors * * @param players */ public BasketballTeam(Player[] players) { this.players = players; } /** * @return the players
  • 6. */ public Player[] getPlayers() { return players; } /** * @param players * the players to set */ public void setPlayers(Player[] players) { this.players = players; } @Override public String toString() { StringBuffer sb = new StringBuffer(); for (Player player : players) { sb.append(player + " "); } return sb.toString(); } @Override public boolean equals(Object obj) { if (obj != null) { if (obj instanceof BasketballTeam) { BasketballTeam another = (BasketballTeam) obj; if (this.getPlayers().length == another.getPlayers().length) { for (int i = 0; i < this.getPlayers().length; i++) { if (!this.players[i].equals(another.players[i])) break; } return true; } } } return false; } /**
  • 7. * Checks if all positions are different, returns true if they are, false if * they are not. * * @return */ public boolean isDifferentPosition() { for (int i = 0; i < this.getPlayers().length - 1; i++) { for (int j = i + 1; j < this.getPlayers().length; j++) { if (this.players[i].getPosition().equals(this.players[j].getPosition())) return false; } } return true; } /** * Returns the team's shooting percentage * * @return */ public double shootingPercentage() { int teamShotsMade = 0; int teamShotsTaken = 0; for (Player player : players) { teamShotsMade += player.getShotsMade(); teamShotsTaken += player.getShotsTaken(); } return ((double) teamShotsMade / teamShotsTaken); } /** * Checks whether the team has a center (that is, the name of the position) * on the team. If yes returns true; otherwise, it returns false * * @return */ public boolean isCenterPresent() { for (Player player : players) {
  • 8. if (player.getPosition().equalsIgnoreCase("Center")) return true; } return false; } /** * Swaps 2 players * @param p1 * @param p2 */ private void swap(Player p1, Player p2) { Player temp = new Player(p1.getName(), p1.getPosition(), p1.getShotsTaken(), p1.getShotsMade()); p1.setName(p2.getName()); p1.setPosition(p2.getPosition()); p1.setShotsMade(p2.getShotsMade()); p1.setShotsTaken(p2.getShotsTaken()); p2.setName(temp.getName()); p2.setPosition(temp.getPosition()); p2.setShotsMade(temp.getShotsMade()); p2.setShotsTaken(temp.getShotsTaken()); } /** * Sorts the array of Player objects in ascending order using the number of * shots taken as the sorting key. */ public void sortByShotsTaken() { for (int i = 0; i < this.players.length - 1; i++) { for (int j = i + 1; j < this.players.length; j++) { if (this.players[i].getShotsTaken() > this.players[j].getShotsTaken()) { swap(this.players[i], this.players[j]); } }
  • 9. } } /** * Returns the array of Player objects sorted in ascending alphabetical order by name. * @return */ public Player[] sortByName() { // Create copy os this array Player[] sortedArr = new Player[this.players.length]; for (int i = 0; i < sortedArr.length; i++) { sortedArr[i] = new Player(this.players[i].getName(), this.players[i].getPosition(), this.players[i].getShotsTaken(), this.players[i].getShotsMade()); } for (int i = 0; i < sortedArr.length - 1; i++) { for (int j = i + 1; j < sortedArr.length; j++) { if (sortedArr[i].getName().compareTo(sortedArr[j].getName()) > 0) { swap(sortedArr[i], sortedArr[j]); } } } return sortedArr; } /** * Returns the name of the player with the most shots made. */ public String playerWithMostShotsMade() { int index = 0; int max = this.players[0].getShotsMade(); for (int i = 1; i < this.players.length - 1; i++) { if(max < this.players[i].getShotsMade()) { max = this.players[i].getShotsMade(); index = i;
  • 10. } } return this.players[index].getName(); } /** * Returns the name of the player with the highest shooting average */ public String playerWithHighestShootingAvg() { int index = 0; double max = this.players[0].shootingPercentage(); for (int i = 1; i < this.players.length; i++) { double avg = this.players[i].shootingPercentage(); if(max < avg) { max = avg; index = i; } } return this.players[index].getName(); } } /** * This class tests the methods of BasketballTeam class * * @author * */ public class BasketballTeamClient { public static void main(String[] args) { // Create Player array with 5 objects Player[] players = new Player[5]; players[0] = new Player("Bill", "Center", 10, 2); players[1] = new Player("Joe", "Point guard", 10, 7); players[2] = new Player("Andrew", "Shooting guard", 20, 2); players[3] = new Player("James", "Power forward", 20, 12); players[4] = new Player("Ben", "Small forward", 10, 4);
  • 11. // Test the constructor BasketballTeam team = new BasketballTeam(players); // Display team System.out.println("Team: " + team); System.out.println(); // Check if all positions are different System.out.println("Are all positions different? " + (team.isDifferentPosition() ? "Yes" : "No")); System.out.println(); // Team Shooting Percentage System.out.println("Team Shooting Percentage: " + team.shootingPercentage()); System.out.println(); // Check if center position is present System.out.println("Is center player available: " + (team.isCenterPresent() ? "Yes" : "No")); System.out.println(); // Sort array using the number of shots taken team.sortByShotsTaken(); System.out.println("Sorted team using the number of shots taken as the sorting key: " + team); System.out.println(); // Sort team in ascending alphabetical order by name. Player[] sortedArr = team.sortByName(); System.out.println("Sorted team in ascending alphabetical order by name: " ); for (Player player : sortedArr) { System.out.println(player); } System.out.println(); // Player with the most shots made. System.out.println("Player with the most shots made: " + team.playerWithMostShotsMade()); System.out.println(); // Player with the highest shooting average. System.out.println("Player with the highest shooting average: " + team.playerWithHighestShootingAvg()); }
  • 12. } SAMPLE OUTPUT: Team: Player name: Bill Position: Center Shots Taken: 10 Shots Made: 2 Player name: Joe Position: Point guard Shots Taken: 10 Shots Made: 7 Player name: Andrew Position: Shooting guard Shots Taken: 20 Shots Made: 2 Player name: James Position: Power forward Shots Taken: 20 Shots Made: 12 Player name: Ben Position: Small forward Shots Taken: 10 Shots Made: 4 Are all positions different? Yes Team Shooting Percentage: 0.38571428571428573 Is center player available: Yes Sorted team using the number of shots taken as the sorting key: Player name: Bill Position: Center Shots Taken: 10 Shots Made: 2 Player name: Joe Position: Point guard Shots Taken: 10 Shots Made: 7 Player name: Ben Position: Small forward Shots Taken: 10 Shots Made: 4 Player name: James Position: Power forward Shots Taken: 20 Shots Made: 12 Player name: Andrew Position: Shooting guard Shots Taken: 20 Shots Made: 2 Sorted team in ascending alphabetical order by name: Player name: Andrew Position: Shooting guard Shots Taken: 20 Shots Made: 2 Player name: Ben Position: Small forward Shots Taken: 10 Shots Made: 4 Player name: Bill Position: Center Shots Taken: 10 Shots Made: 2
  • 13. Player name: James Position: Power forward Shots Taken: 20 Shots Made: 12 Player name: Joe Position: Point guard Shots Taken: 10 Shots Made: 7 Player with the most shots made: James Player with the highest shooting average: Joe