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

I NEED THE WHOLE PROGRAM!! NOT JUST THE MAIN PROGRAMMING!!! IN JAVA!! Calculate

ID: 3694554 • Letter: I

Question

I NEED THE WHOLE PROGRAM!! NOT JUST THE MAIN PROGRAMMING!!! IN JAVA!!

Calculate a player’s batting statistics

Console

Operation

This application calculates the batting average and slugging percentage for one or more baseball or softball players.

For each player, the application first asks for the number of at bats. Then, for each at bat, the application asks for the result.

To enter an at-bat result, the user enters the number of bases earned by the batter. If the batter was out, the user enters 0. Otherwise, the user enters 1 for a single, 2 for a double, 3 for a triple, or 4 for a home run.

After all the at-bat results are entered, the application displays the batting average and slugging percent.

Specifications

The batting average is the total number of at bats for which the player earned at least one base divided by the number of at bats.

The slugging percentage is the total number of bases earned divided by the number of at bats.

Use an array to store the at-bat results for a player.

Validate the input so the user can enter only positive integers. For the at-bat results, the user’s entry must be 0, 1, 2, 3, or 4.

Validate the user’s response to the question “Another batter?” so the user can enter only Y, y, N, or n. If the user enters Y or y, calculate the statistics for another batter. Otherwise, end the program.

Format the batting average and slugging percent to show three decimal digits.

Enhancements

At the start of the program, prompt the user for the number of batters to enter. Then, save the statistics for all of the batters in a two-dimensional array. The program won’t have to ask the user whether to enter data for another batter, since it will know how many batters are to be entered. After all batters have been entered, print a one-line summary for each batter:

Instead of storing an array of integers, create a class named AtBat and store instances of this class in the array. This class should define an enumeration named Result with members OUT, SINGLE, DOUBLE, TRIPLE, and HOMERUN. The class should have a constructor that accepts a Result parameter and a method named basesEarned that returns an int representing the number of bases earned for the at bat.

I NEED THE WHOLE PROGRAM!! NOT JUST THE MAIN PROGRAMMING!!! IN JAVA!!!!

Explanation / Answer


import java.util.Scanner;

public class BattingApp {
   public static void main(String[] args) {
       int k = 0;
       Scanner sc = new Scanner(System.in);
       String answer = "";
       do
       {
       int atBats = Validator.getInt(sc, "Enter the number of times at bat");
       Batter b = new Batter(atBats);
       System.out.println("0=out, 1=single, 2=double, 3=triple, 4=home run");
       for (int i = 0; i < atBats; i++) {
           k = Validator.getIntWithinRange(sc, "Result for at bat " + (i + 1),
                   0, 4);
           b.addBats(k);
       }
       System.out.println("Batting Average " + b.formatNumber(b.calcBattingAverage()));
       System.out.println("Slugging Percentage " + b.formatNumber(b.calcSluggingPercent()));
       answer = Validator.getAnswer(sc, "Another Batter? (y/n)");
       }
       while(answer.equalsIgnoreCase("y"));
   }
}


Batter.java

import java.text.NumberFormat;
import java.util.ArrayList;

public class Batter {
   ArrayList<Integer> a;
   int atBats;
   double battingAverage;
   double sluggingPercent;
  
   public Batter()
   {
      
   }
  
   public Batter(int atBats)
   {
       this.atBats = atBats;
       a = new ArrayList<Integer>();
   }
  
   public void addBats(int k)
   {
       a.add(k);
   }
  
   public double calcBattingAverage()
   {
       int scoreCount = 0;
       for(Integer in : a)
       {
           if(in > 0)
           {
               scoreCount++;
           }
       }
       return (double)scoreCount / atBats;
   }
  
   public double calcSluggingPercent()
   {
       int basesEarned = 0;
       for(Integer in : a)
       {
           basesEarned += in;
       }
       return (double)basesEarned / atBats;  
   }
  
   public String formatNumber(double d)
   {
       NumberFormat percent = NumberFormat.getNumberInstance();
       percent.setMaximumFractionDigits(3);
       percent.setMinimumFractionDigits(3);
       return percent.format(d);
   }
}


Validator.java

import java.util.Scanner;

public class Validator {

   public static String getAnswer(Scanner sc, String prompt) {
       String answer = "";
       System.out.println(prompt);
       while (true) {
           answer = sc.nextLine();
           if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n"))
               return answer;
           else
               System.out.println("Error: Please enter y or n");
       }

   }
  
  
   public static String getString(Scanner sc, String prompt) {
       System.out.println(prompt);
       String s = sc.next();
       sc.nextLine();
       return s;
   }
  
   public static int getInt(Scanner s, String prompt)
   {
       int input = 0;
       while(true)
       {
           System.out.println(prompt);
       if(s.hasNextInt())
       {
           input = s.nextInt();
           s.nextLine();
           return input;
       }
       else
       {
           s.nextLine();
           System.out.println("Error: please enter an integer");
       }
       }
      
   }

   public static int getIntWithinRange(Scanner s, String prompt, int min, int max) {
       int input = 0;
       while(true)
       {
       input = getInt(s, prompt);
       if(input < min)
           System.out.println("Error: number must be greater than " + input + " try again.");
       else if (input > max)
           System.out.println("Error: number must be less than " + input + " try again.");
       else
           return input;
       }
   }
}

sample output

Enter the number of times at bat                                                                                                                            
5                                                                                                                                                           
0=out, 1=single, 2=double, 3=triple, 4=home run                                                                                                             
Result for at bat 1                                                                                                                                         
1                                                                                                                                                           
Result for at bat 2                                                                                                                                         
2                                                                                                                                                           
Result for at bat 3                                                                                                                                         
3                                                                                                                                                           
Result for at bat 4                                                                                                                                         
2                                                                                                                                                           
Result for at bat 5                                                                                                                                         
4                                                                                                                                                           
Batting Average 1.000                                                                                                                                       
Slugging Percentage 2.400                                                                                                                                   
Another Batter? (y/n)                                                                                                                                       
n