Write a class BasketballTeam encapsulating the concept of a
 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() + \"\\t\\tShots 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








