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: 3811539 • 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

JAVA :

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Player{
   private String name, conference;
   private int height;
  
   public Player(){
       name = "TBD";
       conference = "TBD";
       height = 0;
   }
   public Player(String name, String conference, int height){
       this.name = name;
       this.conference = conference;
       this.height = height;
   }
   public String getName(){
       return this.name;
   }
   public String getConference(){
       return this.conference;
   }
   public int getHeight(){
       return this.height;
   }
   @Override
   public String toString(){
       return name+" "+conference+" "+height;
   }
}

class Team{
   private Player[] myTeam;
   private int nextOpenPos;
   Team(){
       myTeam = new Player[3];
       nextOpenPos = 0;
   }
   public void addPlayer(Player p){
       myTeam[nextOpenPos] = p;
       nextOpenPos++;
   }
   Player[] getCurrentTeamMembers(){
       int ps = 0;
       for(int i=0;i<3;i++){
           if(myTeam[i]!=null){
               ps++;
           }
       }
       Player[] ans = new Player[ps];
       int ind = 0;
       for(int i=0;i<3;i++){
           if(myTeam[i]!=null){
               ans[ind++] = myTeam[i];
           }
       }
       return ans;
   }
   public double averageHeight(){
       double h = 0;
       int c = 0;
       for(int i=0;i<3;i++){
           if(myTeam[i]!=null){
               h+=myTeam[i].getHeight();
               c++;
           }
       }
       return h/c;
   }
   @Override
   public String toString(){
       double avg = this.averageHeight();
       String ans = "Total "+nextOpenPos+" Average Height : "+((int)(avg/12))+"."+(((int)avg)%12);
       for(int i=0;i<3;i++){
           if(myTeam[i]!=null){
               ans = ans + " Name : "+myTeam[i].getName()+" Conference : "+myTeam[i].getConference()+" Height : "+myTeam[i].getHeight();
           }
       }
       return ans;
   }
}

class Game{
   private static Scanner obj;
   private static Team fillRoster(){
       obj = new Scanner(System.in);
       System.out.println("Enter number of players");
       int n = obj.nextInt();
       Team t = new Team();      
       String name, conference;
       int height;
       Player p;
       if(n>=1){
           System.out.println("Enter player 1 details");
           name = obj.next();
           conference = obj.next();      
           height = obj.nextInt();      
           p = new Player(name,conference,height);
           t.addPlayer(p);
       }
       if(n>=2){
           System.out.println("Enter player 2 details");
           name = obj.next();
           conference = obj.next();
           height = obj.nextInt();
           p = new Player(name,conference,height);
           t.addPlayer(p);
       }
       if(n>=3){
           System.out.println("Enter player 3 details");
           name = obj.next();
           conference = obj.next();
           height = obj.nextInt();
           p = new Player(name,conference,height);
           t.addPlayer(p);
       }
       return t;
   }
  
   public static void main(String args[]){
       Team home = fillRoster();
       Team visitor = fillRoster();
       double homeHeight = home.averageHeight();
       double visitorHeight = visitor.averageHeight();
       if(homeHeight>visitorHeight){
           System.out.println("Home team is taller");
           System.out.println(home);
       }
       else{
           System.out.println("Visitor team is taller");
           System.out.println(visitor);
       }
   }
}

OUTPUT :

D:>java Ideone
Enter number of players
1
Enter player 1 details
Rajesh
Oracle
71
Enter number of players
2
Enter player 1 details
Praveen
Airvana
69
Enter player 2 details
Venkat
Algo
68
Home team is taller
Total 1 Average Height : 5.11 Name : Rajesh Conference : Oracle Height : 71

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