Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that keeps track of the players in a 3x3 basketball game. Your p

ID: 3812445 • Letter: W

Question

Write a program that keeps track of the players in a 3x3 basketball game. Your program should have the following classes:


A Player class. A Player has

instance variables for name, conference (e.g. "ACC",
"Big Ten", "Big 12", etc.)
and height. (Keep height in inches; use an int.)

overloaded constructors

A constructor that takes name, conference and height parameters

A no-argument constructor which sets name="TBD", conference="TBD", and height=0.

getters and setters for name, conference and height

a String toString() method that returns a player's data as a String object

A Team class. A Team has

an instance variable that is an array of the team's players (i.e., Player[] myTeam)

an instance variable that indicates the "next open" position of the team array. You may assume that no more than 3 players will be added to a team, but a team might have less than 3 players.

a no-argument constructor that initializes the player array

a method void add(Player player) that adds a player to the team

a method Player[] getCurrentTeamMembers() that returns a player array of all the players currently on the team (note that the team may not have a full roster when this method is called. You should return an array with no "empty" slots.)

a method double averageHeight() that returns the average height (in inches) of all the players currently on the team. Your code should handle a team with no members.

a String toString() method that returns the number of players currently on the team, average height in feet and inches and their names, conferences and heights


A Game class. A Game class has

a private static Scanner class variable for reading user input from the keyboard

a method private static Team fillRoster() that uses the Scanner to query the user for all of the players on a Team.

a main method. The main method

has two Team variables, home and visitor

prompts the user to enter data for the home team, and then calls fillRoster to create the Team object

prompts the user to enter data for the visiting team, and then calls fillRoster to create the Team object

prints a message identifying the team (home or visitor) that has the taller average height

prints the taller team's roster

prints the shorter team's roster

A sample execution of the program is attached. Use that data to test your submission.

Explanation / Answer

Please find the Java Programs for the given problem.

Find all three Java Files, Player.java, Team.java and Game.java

Compiled and run in Eclipse.

--------------------------------------------------------------
Player.java::
--------------------------------------------------------------

public class Player {
   String name;
   String conference;
   int height;
  
   Player(){
       this.name = "TBD";
       this.conference = "TBD";
       this.height = 0;
   }
  
   Player(String name, String conference, int height){
       this.name = name;
       this.conference = conference;
       this.height = height;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getConference() {
       return conference;
   }

   public void setConference(String conference) {
       this.conference = conference;
   }

   public int getHeight() {
       return height;
   }

   public void setHeight(int height) {
       this.height = height;
   }

   @Override
   public String toString() {
       return "Player [name=" + name + ", conference=" + conference + ", height=" + height + "]";
   }

}


--------------------------------------------------------------
Team.java::
--------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;

public class Team {
   private List<Player> myTeam;
   private int nextOpen;
  
   Team(){
       this.myTeam = new ArrayList<Player>(3);
   }
  
   public void add(Player player){
       myTeam.add(player);
       this.nextOpen += 1;
   }
  
   public List<Player> getCurrentTeamMembers(){
       return this.myTeam;
   }
  
   public double averageHeight(){
       int heightSum = 0;
       int numPlayer = 0;
       double averageHeight = 0;
       for(Player player : myTeam){
           heightSum += player.getHeight();
           numPlayer++;
       }
       averageHeight = heightSum/numPlayer;
       return averageHeight;
   }
  
   private String inchToFeet(double averageHeight) {
       int feet = (int) (averageHeight / 12);
       int leftOver = (int) (averageHeight % 12);
       String height = feet+"'"+leftOver+"''";
       return height;
   }

   @Override
   public String toString() {
       return "Team numPlayers=" + myTeam.size() + ",Average Height"+inchToFeet(averageHeight())+", Members="+myTeam.toString();
   }
  
}

--------------------------------------------------------------
Game.java::
--------------------------------------------------------------

import java.util.Scanner;

public class Game {
   private static Scanner scanner;
   private static Team fillRoaster(){
       scanner = new Scanner(System.in);
       Team team = new Team();
       for(int i=1;i<=3;i++){
           System.out.println("Enter team member : "+i);
           Player player = new Player();
           player.setName(scanner.nextLine());
           player.setConference(scanner.nextLine());
           player.setHeight(Integer.valueOf(scanner.nextLine()));
           team.add(player);
       }
       return team;
   }
  
   public static void main(String[] args){
       scanner = new Scanner(System.in);
       System.out.println("Enter the home team");
       Team homeTeam = fillRoaster();
      
       System.out.println("Enter the visiting team");
       Team visitingTeam = fillRoaster();
      
       double averageHeightHome = homeTeam.averageHeight();
       double averageHeightVisiting = visitingTeam.averageHeight();
       if(averageHeightHome > averageHeightVisiting){
           System.out.println("The home team is taller");
           System.out.println("Taller team: "+homeTeam.toString());
           System.out.println("Shorter team: "+visitingTeam.toString());
       } else {
           System.out.println("The visiting team is taller");
           System.out.println("Taller team: "+visitingTeam.toString());
           System.out.println("Shorter team: "+homeTeam.toString());
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote