Question: I need help with Array Lists. I need these requirements: -Use an ArrayList to store the at-bat results for a player -Accept integer values of
I need help with Array Lists. I need these requirements:
-Use an ArrayList to store the at-bat results for a player
-Accept integer values of 1-10 of number of at bats
-Accept integer values of 0,1,2,3,or 4 for at bat results
-Make the Another batter? Prompt required
-Store validation code in separate methods (or use a Validation class)
-Display appropriate error messages if user enters invalid data
-Use NumberFormat to display the batting average and slugging percent with 3 decimal places.
-Include a welcome message and comments
Calculations:
-The batting average is the total number of hits (entries of 1, 2, 3, or 4) divided by the number of at bats.
-The slugging percentage is the total number of bases divided by the number of at bats
-Store the batting average and slugging percent calculations as double..you will have to cast one of your int operands to make this happen.
This is what the results should look like:

This is the code I have so far:
// import statments
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;
//class header
class BattingAverageAppMGT
{
// main() method header
public static void main(String []arg)
{
//Welcome user to the app
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Batting Average Calculator. ");
while(true)
{
// Ask user to enter data
System.out.print("Enter the number of batters: ");
int n = sc.nextInt();
System.out.println(" 0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run");
int [][]batterScore = new int[n][3];
for(int i=0;i { System.out.println(" Enter statistics for batter " + (i+1) + "..."); for(int j=0;j { System.out.print("Result for at-bat " + (j+1) + ": "); batterScore[i][j] = sc.nextInt(); } } //print results System.out.println(); for(int i=0;i { int count = 0; int sum = 0; for(int j=0;j { if(batterScore[i][j] > 0) { count ++; } sum += batterScore[i][j]; } DecimalFormat numFormat = new DecimalFormat("#0.000"); System.out.println("Batter " + (i+1) + " average: " + numFormat.format(((double)count)/3) + " \t\tSlugging percent: " + numFormat.format(((double)sum)/3)); } // ask if user wants to continue System.out.print(" Another Batter? (y): "); String s = sc.next(); if(s.charAt(0) == 'n' || s.charAt(0) == 'N') { break; } }//end while loop }//end main() method }// end class BattingAverageApp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
