In JAVA, Write 2 classes encapsulating the concept of statistics for a baseball
ID: 659657 • Letter: I
Question
In JAVA, Write 2 classes encapsulating the concept of statistics for a baseball team
A player class that has the attributes player name, at bats, hits, and average. (I Made It already) Need Help With Team Info*
A Team info class which has the following attributes: a team name, totals hits, team average, player array, max player, player count
Needs to include:
- A constructor method
- add player method
- update stats
- get stats
- to string
PLAYER CLASS
/**********************************************************************
*Program Name :
*Author :
*Due Date :
*Course/Section :
*Program Description: Write a program to create an array based on a
*
*
*Constructor: initializes the instance data
*updateStats: adds hits and at bats to the totals in the instance data
*toString : formats the instance data for display
*********************************************************************/
import java.text.*;
public class BaseballPlayer
{
//class constants
//class variables
private String name; //players first and last name
private int hits; //players hit total
private int atBats; //number of attempts
private float avg; //players batting average
/**********************************************************************
*Method Name : BaseballPlayer(constructor)
*Author :
*Due Date :
*Course/Section :
*Program Description: This constructor will intialize the instance data
* to values passed in via the parameter list.
*
*BEGIN BaseballPlayer
* initialize all instace data
*END Baseball Player
***********************************************************************/
public BaseballPlayer(String inName, int inHits, int inAtBats)
{
//local constants
//local variables
/******************************************************/
//initialize the instance data
name = inName;
atBats = inBats;
hits = inHits;
avg = (float)hits / atBats;
}//end constructor
/**********************************************************************
*Method Name : Update Stats
*Author :
*Due Date :
*Course/Section :
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN update stats(hits, at bats)
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END Baseball Player
***********************************************************************/
public void updateStats(int inAtBats, int inHits)
{
//local constants
//local variables
/*******************************************************************/
//update player at bats
atBats += inAtBats;
//update player hits
hits += inHits;
//recalculate the batting average
avg = (float)hits / atBats;
}//end updateStats
/**********************************************************************
*Method Name : toString
*Author :
*Due Date :
*Course/Section :
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN BaseballPlayer
* IF(player name is too long)
* make it 10 characters long
* ELSE
* padd the name to be 10 characters long
* END IF
* Create output string
* return output string
*
*END Baseball Player
***********************************************************************/
public String toString()
{
//local constants
final int NAME_LENGTH = 10;
//local variables
String fmtName; //name from instance data formatted to NAME_LENGTH
String output; //formatted instance data
DecimalFormat fmt = new DecimalFormat("0.000");
String avgFmt = fmt.format(avg);
/*******************************************************************/
//IF(player name is too long)
if (name.length() > NAME_LENGTH)
//make it 10 characters long
fmtName = name.substring(0,10);
//ELSE name is not too long
else
//padd the name to be 10 characters long
fmtName = Util.setRight(NAME_LENGTH, name);
//END IF
//Create output string
output = Util.setLeft(29, "Name : ") + fmtName + " " +
Util.setLeft(29, "At Bats:: ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Hits : ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Avg : ") + Util.setRight(NAME_LENGTH + 5, "" + avgFmt);
//return output string
return output;
}//end toString
}//end BaseballPlayer class
Explanation / Answer
I try to complete "TeamInfo" class. Revert back if you have any doubts.
//Create a class TeamInfo
public class TeamInfo
{
// Declare variables
private int[] atBats;
private int[] hits;
int numberOfPlayers;
// constructor
public TeamInfo(int[] startHits, int[] startAtBats, int num) {
atBats = startAtBats;
hits = startHits;
numberOfPlayers = num;
}
//add player method
public int getNumberOfPlayers()
{
return numberOfPlayers;
}
// accessor for atBats
// return aray with number of at-bats per player
public int[] getAtBats()
{
int[] temp = new int[numberOfPlayers];
for (int i = 0; i < atBats.length; i++)
{
temp[i] = atBats[i];
}
return temp;
}
// accessor for hits
// return totals hits
public int[] getHits()
{
int[] temp = new int[numberOfPlayers];
for (int i = 0; i < hits.length; i++)
{
temp[i] = hits[i];
}
return temp;
}
// mutator for atBats
//to max player
public void setAtBats(int[] newAtBats)
{
atBats = new int[newAtBats.length];
for (int i = 0; i < atBats.length; i++)
atBats[i] = newAtBats[i];
}
// mutator for hits
// @ param newHits number of times each player batted
public void setHits(int[] newHits)
{
hits = new int[newHits.length];
}
// toString
// return number of players, at bats, and hits
public String toString() {
String returnString = " ";
for (int i = 0; i < hits.length; i++)
returnString = returnString + (i++) + " " + hits[i] + " "
+ atBats[i] + " ";
return returnString;
}
// return true if the numberOfPlayers, atBats, and hits in this object
// are equal to those in parameter object; false otherwise
public boolean equals(Object o) {
if (!(o instanceof BaseballClient))
return false;
else {
BaseballClient b = (BaseballClient) o;
if (numberOfPlayers != b.getNumberOfPlayers())
return false;
}
return true;
}
// batting averages
// @return array of batting averages for each player
public double[] battingAverages() {
double[] temp = new double[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++) {
if (atBats[i] == 0)
temp[i] = 0;
else
temp[i] = (double) (hits[i] / atBats[i]);
}
return temp;
}
// totalHits
// @ return total number of hits for the team
public int totalHits()
{
int total = 0;
for (int i = 0; i < numberOfPlayers; i++) {
total = total + hits[i];
}
return total;
}
// good players
// @ return number of players with batting
public int playerArray() {
double average[] = battingAverages();
int count = 0;
for (int i = 0; i < numberOfPlayers; i++) {
if (average[i] > 0.300) {
count++;
}
}
return count;
}
private static int maxPlayer(int[] array, int size) {
int index = 0;
for (int i = 0; i < size; i++) {
if (array[i] > array[index])
index = i;
}
return index;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.