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

Java Programming- Goals The lab this lesson introduces students to Object Orient

ID: 3809900 • Letter: J

Question

Java Programming-

Goals

The lab this lesson introduces students to Object Oriented Thinking. By the end of this lab, students should be able to

To apply class abstraction to develop software

To discover the relationships between classes

To design programs using the object-oriented paradigm

To use the String class to process immutable strings

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 and height. (Keep height in inches; use an int.)

overloaded constructors

A constructor that takes name and height parameters

A no-argument constructor

getters and setters for name 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 int 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, and their names 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

uses fillRoster to create the Team object and its associated Player objects for the home team

uses fillRoster to create the Team object and its associated Player objects for the visting team

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-

Answer-

Player.java:

import java.util.Scanner;


public class Game {
   private static Scanner s = new Scanner(System.in);
  
   private static Team fillRoster(){
       Team team = new Team();
       for(int i=0; i<3; i++) {
           System.out.println("Enter team member " + (i+1) + ": ");
           System.out.print("Name: ");
           String name = s.next();
           System.out.print("Height: ");
           int height = Integer.parseInt(s.next());
           Player p = new Player(name, height);
           team.addPlayer(p);
       }
       return team;
   }
  
   public static void main(String strargs[]) {
       Team home, visitor;
       System.out.println("Enter Home team:");
       home = fillRoster();
       System.out.println("Enter Visitor team:");
       visitor = fillRoster();

       if(home.averageHeight() > visitor.averageHeight()) {
           System.out.println("Home Team is taller.");
           System.out.println("Taller team roster: " + home);
           System.out.println("Shorter team roster: " + visitor);
       }else if(home.averageHeight() < visitor.averageHeight()) {
           System.out.println("Visitor Team is taller.");
           System.out.println("Taller team roster: " + visitor);
           System.out.println("Shorter team roster: " + home);
       }
          
   }
}



Team.java:

import java.util.Arrays;

public class Team {
   private Player[] myTeam;
   int next_open = 0;

   public Team() {
       this.myTeam = new Player[3];
       this.next_open = 0;
   }

   public void addPlayer(Player p) {
       if (next_open == 3)
           return;
       myTeam[next_open++] = p;
   }

   public Player[] getCurrentTeamMembers() {
       return Arrays.copyOf(myTeam, next_open);
   }

   public double averageHeight() {
       int sumHeights = 0;
       for (int i = 0; i < next_open; i++) {
           sumHeights += myTeam[i].getHeight();
       }
       return sumHeights / 3.0;
   }

   @Override
   public String toString() {
       StringBuilder sb = new StringBuilder();
       sb.append("There are " + next_open + " players in team: ");
       for(int i=0; i< next_open; i++) {
           sb.append(myTeam[i] + " ");
       }
       return sb.toString();
   }
  
  
}


Game.java:

import java.util.Scanner;


public class Game {
   private static Scanner s = new Scanner(System.in);
  
   private static Team fillRoster(){
       Team team = new Team();
       for(int i=0; i<3; i++) {
           System.out.println("Enter team member " + (i+1) + ": ");
           System.out.print("Name: ");
           String name = s.next();
           System.out.print("Height: ");
           int height = Integer.parseInt(s.next());
           Player p = new Player(name, height);
           team.addPlayer(p);
       }
       return team;
   }
  
   public static void main(String strargs[]) {
       Team home, visitor;
       System.out.println("Enter Home team:");
       home = fillRoster();
       System.out.println("Enter Visitor team:");
       visitor = fillRoster();

       if(home.averageHeight() > visitor.averageHeight()) {
           System.out.println("Home Team is taller.");
           System.out.println("Taller team roster: " + home);
           System.out.println("Shorter team roster: " + visitor);
       }else if(home.averageHeight() < visitor.averageHeight()) {
           System.out.println("Visitor Team is taller.");
           System.out.println("Taller team roster: " + visitor);
           System.out.println("Shorter team roster: " + home);
       }
          
   }
}

Please include the player class in the answer above as it is missing.

Explanation / Answer

Hi I have included the missing Player.class hope it helps

//Player.java:
import java.util.Scanner;


public class Player {

//Name of Player
private String name;

//Height in inches. Takes int
private int height;
  
//No Arguments Constructor
public Player() {
}

//Takes name and height as arguments
public Player(String name, int height) {
this.name = name;
this.height = height;
}
  
  
  
  
public String getName() {
return name;
}


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


public int getHeight() {
return height;
}

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

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

  
}

Team.java:

import java.util.Arrays;

public class Team {
   private Player[] myTeam;
   int next_open = 0;

   public Team() {
       this.myTeam = new Player[3];
       this.next_open = 0;
   }

   public void addPlayer(Player p) {
       if (next_open == 3)
           return;
       myTeam[next_open++] = p;
   }

   public Player[] getCurrentTeamMembers() {
       return Arrays.copyOf(myTeam, next_open);
   }

   public double averageHeight() {
       int sumHeights = 0;
       for (int i = 0; i < next_open; i++) {
           sumHeights += myTeam[i].getHeight();
       }
       return sumHeights / 3.0;
   }

   @Override
   public String toString() {
       StringBuilder sb = new StringBuilder();
       sb.append("There are " + next_open + " players in team: ");
       for(int i=0; i< next_open; i++) {
           sb.append(myTeam[i] + " ");
       }
       return sb.toString();
   }
  
  
}

Game.java:

import java.util.Scanner;


public class Game {
   private static Scanner s = new Scanner(System.in);
  
   private static Team fillRoster(){
       Team team = new Team();
       for(int i=0; i<3; i++) {
           System.out.println("Enter team member " + (i+1) + ": ");
           System.out.print("Name: ");
           String name = s.next();
           System.out.print("Height: ");
           int height = Integer.parseInt(s.next());
           Player p = new Player(name, height);
           team.addPlayer(p);
       }
       return team;
   }
  
   public static void main(String strargs[]) {
       Team home, visitor;
       System.out.println("Enter Home team:");
       home = fillRoster();
       System.out.println("Enter Visitor team:");
       visitor = fillRoster();

       if(home.averageHeight() > visitor.averageHeight()) {
           System.out.println("Home Team is taller.");
           System.out.println("Taller team roster: " + home);
           System.out.println("Shorter team roster: " + visitor);
       }else if(home.averageHeight() < visitor.averageHeight()) {
           System.out.println("Visitor Team is taller.");
           System.out.println("Taller team roster: " + visitor);
           System.out.println("Shorter team roster: " + home);
       }
          
   }
}

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