You will: Use the given program template that has the main arrays populated with
ID: 3674465 • Letter: Y
Question
You will:
Use the given program template that has the main arrays populated with data
Process the arrays by calculating each player’s batting average (hits / at bats) and store their batting average in a separate array. Note batting average array stores doubles, both at bats and hits arrays are integers so think about what you have to do so the result of the division stores a double.
Show each player name, their at bats, their hits, and their batting average
Show who had the worst batting average and what that average is
Show who had the best batting average and what that average
Template:
public static void main(String[] args)
{
final int NUMBER_OF_PLAYERS = 9;
String[] playerName = {"Carew", "Powell", "Bera", "Cobb", "Jones", "Ripken", "Munson", "White", "Boggs"};
int[] atBats = {15, 20, 8, 22, 20, 22, 21, 22, 14};
int[] hits = {2, 4, 1, 15, 6, 7, 8, 2, 1};
double[] battingAverage = new double[NUMBER_OF_PLAYERS];
int i = 0; // Index for arrays
double minAverage = 999;
double maxAverage = 0;
Explanation / Answer
class batting_avg
{
public static void main(String[] args)
{
final int NUMBER_OF_PLAYERS = 9;
String[] playerName = {"Carew", "Powell",
"Bera", "Cobb", "Jones", "Ripken", "Munson", "White",
"Boggs"};
int[] atBats = {15, 20, 8, 22, 20, 22, 21, 22, 14};
int[] hits = {2, 4, 1, 15, 6, 7, 8, 2, 1};
double[] battingAverage = new double
[NUMBER_OF_PLAYERS];
int i,x = 0;
double minAverage = 999;
double maxAverage = 0;
for(i=0;i<9;i++)
{
battingAverage[i]=atBats[i]/hits[i];
}
for(i=0;i<9;i++)
{
System.out.println(playerName
[i]+" "+atBats[i]+" "+hits[i]+" "+battingAverage[i]);
}
minAverage=battingAverage[0];
for(i=0;i<9;i++)
{
if(battingAverage[i]<minAverage)
{
x=i;
minAverage=battingAverage[i];
}
}
System.out.println("Worst Batting Average is =
"+battingAverage[x]+"of "+playerName[x]);
maxAverage=0;
for(i=0;i<9;i++)
{
if(battingAverage[i]>minAverage)
{
x=i;
minAverage=battingAverage[i];
}
}
System.out.println("Best Batting Average is =
"+battingAverage[x]+"of "+playerName[x]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.