class encapsulating the concept of statistics for a baseball team, which has the
ID: 3709583 • Letter: C
Question
class encapsulating the concept of statistics for a baseball team, which has the following attributes: a number of players, a of number of hits for each player, a list of number of at-bats for each player. list 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. O Accessors, mutators, toString, and ofajs rethodsn O Generate and return an array of batting averages based on the attributes given. O Calculate and return the total number of hits for the team. Calculate and return the number of players with a batting aver- age 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
import java.util.Arrays;
public class Baseball{
private int hits[];
private double at_bats[];
public Baseball(int[] hits, double[] at_bats) {
this.hits = hits;
this.at_bats = at_bats;
}
public int[] getHits() {
return hits;
}
public void setHits(int[] hits) {
this.hits = hits;
}
public double[] getAt_bats() {
return at_bats;
}
public void setAt_bats(double[] at_bats) {
this.at_bats = at_bats;
}
@Override
public String toString() {
return "Baseball [hits=" + Arrays.toString(hits) + ", at_bats=" + Arrays.toString(at_bats) + "]";
}
public double getBattingAvg() {
double sum = 0;
for(int i =0; i < hits.length; i++)
sum += hits[i];
return sum / hits.length;
}
public int getAboveAvg() {
int count = 0;
for(int i =0; i < at_bats.length; i++)
if(at_bats[i] > 0.3)
count++;
return count;
}
public int[] getSortedHits() {
Arrays.sort(hits);
return hits;
}
}
**Comment for any further queries.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.