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

1) Players\' Names array: This is a 1D string array that will hold the names of

ID: 3632866 • Letter: 1

Question

1) Players' Names array: This is a 1D string array that will hold the names of the five players. This array should be initialized in the program with the following data:

Index Name
0 John
1 Will
2 Babe
3 Sammy
4 Cal

2) Home Runs array: This is a 2D integer array of size 5x5 that stores the number of home runs of each player for each of the five games. This array should be initialized with the following data:


Player Game 0 1 2 3 4
0 1 0 1 2 1
1 1 0 0 1 1
2 1 2 3 3 1
3 2 1 0 1 2
4 1 1 1 1 1

3) Walks array: This is a 2D integer array of size 5x5 that stores the number of walks of each player for each of the five games. This array should be initialized with the following data:


Player Game 0 1 2 3 4
0 3 2 1 1 0
1 1 1 2 1 0
2 1 2 1 2 1
3 3 2 3 1 2
4 1 2 1 0 1


Also, your program should perform the following functions:

1) Display player statistics. The program should print out a table of three columns: the player's name, their total home runs, and their total walks (see the "Player Name..." table in the sample output for an example). This can be done by completing the “total” method in the given code.

2) Find the player with the most home runs. The program should find the player who hit the most total home runs. This can be done by completing the “mostIndex” method in the given code.

3) Find the player with the most walks. The program should finds the player with the most total walks. This can be done by completing the “mostIndex” method in the given code. (The method mostIndex can be used for both 2 and 3 by passing different 2-D arrays.)

Here's how the program should look when it's all done (hey, there's no user input this time! :-( ):

Contrived Baseball Statistics Tracking Program (version 1.1)
------------------------------------------------------------

Player Name Home Runs Walks
----------- --------- -----
John 5 7
Will 3 5
Babe 10 7
Sammy 6 11
Cal 5 5

Babe scored the most home runs, with a grand total of 10!
Sammy was walked the most (11 times).

public class BaseBall
{
public static void main(String[] args)
{
/********************************************************
Complete the initialization of the following three arrays
********************************************************/
String[] names ={"John","Will", "Babe","Sammy", "Cal"};

int[][] homeruns =new int[][] {{1,0,1,2,1}, {1,0,0,1,1}, {1,2,3,3,1},{2,1,0,1,2}, {1,1,1,1,1}};

int[][] walks = new int[][] {{3,2,1,1,0}, {1,1,2,1,0}, {1,2,1,2,1},{3,2,3,1,2}, {1,2,1,0,1}};

//Print out the title
System.out.println("Contrived Baseball Statistics Tracking Program"
+ " (Version 1.1)");
System.out.println("-----------------------------"
+"-------------------------------");
System.out.println(" Player Name Home Runs Walks "
+ "----------- ---------- -----");


//Computing the homeruns and walks for each player
int[] homerunsTotal = total(homeruns);
int[] walksTotal = total(walks);

//print out the results
for (int i=0; i<5; i++)
{
System.out.print(names[i]+" "+homerunsTotal[i]
+" "+walksTotal[i]+" ");
}

//Computing the index of the player for the most homeruns and
//and the index of the player for the most walks
int mostHomerunsIndex = mostIndex(homeruns);
int mostWalksIndex = mostIndex(walks);

//Print out the most homeruns player information
System.out.println(" " + names[mostHomerunsIndex]
+ " scored the most home runs, with a grand total of "
+ homerunsTotal[mostHomerunsIndex] + "!");

//Print out the most walks player information
System.out.println(names[mostWalksIndex]
+ " was walked the most ("
+ walksTotal[mostWalksIndex] + " times).");
} //end of main


//The method total takes a 2-D array as a parameter
//and computes the sum of values from columns
//for each row (the first index).
//It stores the results in one dimensional array
//and returns it.
public static int[] total(int[][] array1)
{
/*******************************************************
Complete this method
*******************************************************/
}


//The method mostIndex takes a 2-D array as a parameter,
//computes the row index whose sum of values from columns
//is the largest and returns it.
//You can make use of the total method first to do complete it.
public static int mostIndex(int[][] array2)
{
/*******************************************************
Complete this method
*******************************************************/
}

}

Explanation / Answer

//The method total takes a 2-D array as a parameter
//and computes the sum of values from columns
//for each row (the first index).
//It stores the results in one dimensional array
//and returns it.
public static int[] total(int[][] array1)
{
int t[5];

for(int i = 0; i < 5; i++) {

for(int j = 0; j < 6; j++){

t[i] += array1[i][j];

}

}

return t;

}


//The method mostIndex takes a 2-D array as a parameter,
//computes the row index whose sum of values from columns
//is the largest and returns it.
//You can make use of the total method first to do complete it.
public static int mostIndex(int[][] array2)
{

int a[5] = total(array2);

int max = a[0];

int result;

for(int k = 1; k < 5; k++) {

if(a[k] > max) {

max = a[k];

result = k;

}

}
return result;

}