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

(this is javascript) Write a class encapsulating the concept of statistics for a

ID: 3547380 • Letter: #

Question

(this is javascript)

Write a class encapsulating the concept of statistics for a baseball

team, which has the following attributes: a number of players, a list of

number of hits for each player, a list of number of at-bats for each player.

Write the following methods:

? A constuctor with two equal-length arrays as parameters, the number of hits per player, and the number of at-bats per player.

? Accessors, mutators, toString, and equals methods.

? Generate and return an array of batting averages based on the attributes given.

? Calculate and return the total number of hits for the team.

? Calculate and return the number of players with a batting average greater than .300.

? A method returning an array holding the number of hits,sorted in ascending order.


Write a client class to test all the methods in your class.

Explanation / Answer


public class BaseballClient {

private int numberOfPlayers;

private int [] atBats;
private int []hits;

//constructor
public BaseballClient(int [] startHits, int [] startAtBats, int num)
{
atBats = startAtBats;
hits= startHits;
numberOfPlayers=num;



}

//accessor for numberOfPlayers
//@return number of players
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 array with number of hits per player
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
//@ param newAtBats number of times each player batted
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;

}


//equals
// @ param o another BaseBallClient object
// @ 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.numberOfPlayers)
return false;
for(int i=0; i<atBats.length;i++)
{
if(atBats[i]!=b.atBats[i])
return false;
}

for(int i=0;i<hits.length;i++)
{
if(hits[i]!=b.hits[i])
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 avg>30
public int goodPlayers()
{
double average[] = battingAverages();
int count=0;
for(int i=0;i<numberOfPlayers;i++)
{
if(average[i]>0.300)
{
count++;
}
}
return count;
}
private static int indexOfLargestElement(int [] array, int size)
{
int index=0;
for(int i=0;i<size;i++)
{
if(array[i]>array[index])
index=i;
}
return index;
}
public int[] sortedHits()
{
int [] temp = new int[hits.length];
for(int i=0;i<hits.length;i++)
temp[i]=hits[i];
int max;
for(int i=0;i<temp.length;i++)
{
max=indexOfLargestElement(temp,temp.length-i);
int swapTemp= temp[max];
temp[max]=temp[temp.length-i-1];
temp[temp.length-i-1]=swapTemp;

}
return temp;
}
}