Write a Java program that Reads baseball data in from a comma delimited file. Each line of the file contains a name followed by a list of symbols indicating the result of each at bat: 1 for single, 2 for double, 3 for triple, 4 for home run, o for out, w for walk, s for sacrifice
Statistics are computed and printed for each player.
EXTRA CREDIT (+10 points); compute each player's slugging percentage https://www.wikihow.com/Calculate-Slugging-Percentage
Be sure to avoid a division by zero error
Use the starting code I provide here. Also use the files stats.txt and stats2.txt for test files.
*CODE*
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class BaseballStats {
public static void main (String[] args) throws IOException {
Scanner fileScan, lineScan;
String fileName;
String line; //a line from the file
String playerName;
int numHits, numWalks, numSacrifices, numOuts;
String action;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the name of the input
file: ");
fileName = scan.nextLine();
fileScan = new Scanner (new File(fileName));
// Read and process each line of the file
while (fileScan.hasNext()) {
line = fileScan.nextLine();
lineScan = new Scanner(line);
lineScan.useDelimiter(",");
numHits = 0;
numOuts = 0;
numSacrifices = 0;
numWalks = 0;
playerName = lineScan.next();
while (lineScan.hasNext())
{
action =
lineScan.next();
if
(action.equals("1") || action.equals("2") ||
action.equals("3")
|| action.equals("4"))
numHits++;
else if
(action.equals("o"))
numOuts++;
else if
(action.equals("s"))
numSacrifices++;
// add code to handle counting number of walks
}
//print statistics for the
player
System.out.println ("\nStatistics
for " + playerName + "... ");
System.out.println ("Hits: " +
numHits);
System.out.println ("Outs: " +
numOuts);
System.out.println ("Walks: " +
numWalks);
System.out.println ("Sacrifices: "
+ numSacrifices);
// compute total number of at bats: https://www.wikihow.com/Calculate-a-Batting-Average
// create a DecimalFormat object that limits the number of decimal digits to 3
// output the batting average BUT BE CAREFUL
// consider the situation where a batter has no at bats
// for this condition output "No at bats"
// otherwise output "Batting Average: " followed by the average
formatted as specified above
// EXTRA CREDIT: compute slugging percentage and output similar to batting average
}
}
}
*STAT
Willy Wonk,o,o,1,o,o,o,o,3,w,o,o,o,o,s,1,o,4
Shari Jones,1,o,o,s,s,1,o,o,o,1,o,o,o,o
Barry Bands,2,4,w,o,o,o,w,1,o,o,1,2,o,o,w,w,w,1,o,o
Sally Slugger,o,4,4,o,o,4,4,w
Missy Lots,o,o,1,o,o,w,o,o,o
Joe Jones,o,1,o,o,o,o,1,1,o,o,o,o,w,o,o,o,1,o,1,3
Larry Loop,w,1,o,o,o,1,o,o,1,s,o,o,o,1,1
Sarah Swift,o,o,o,o,1,1,w,o,o,o
Bill Bird,1,o,3,o,1,w,o,o,o,1,s,s,2,o,o,o,o,o,o
Don Daring,o,o,3,3,o,o,3,o,3,o,o,o,o,o,o,3
Jill Jet,o,s,s,1,o,o,1,1,o,o,o,1,o,1,w,o,o,1,1,o
*STAT2
Barry Bands,4,4,w,o,o,o,w,4,o,o,4,4,o,o,w,w,w,4,o,o
Hello Champ!
Here is the solution code in java.
Important notes :
1. Situations, that do not count as "at bats" include walks, hit-by-pitches, sacrifices, etc.
2. A batting average represents the percentage of at bats that result in hits for a particular baseball player
3. Slugging percentage represents the total number of bases a player records per at-bat. Formula for slugging percentage is: (ones*1 + twos*2 + threes*3 + fours*4)/At_bats = total_hit_score/at_bats
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class MyClass {
public static void main (String[] args) throws IOException {
Scanner fileScan, lineScan;
String fileName;
String line; //a line from the file
String playerName;
int numHits, numWalks, numSacrifices, numOuts, hit_score_total;
String action;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter the name of the input file: ");
fileName = scan.nextLine();
fileScan = new Scanner (new File(fileName));
// Read and process each line of the file
while (fileScan.hasNext()) {
line = fileScan.nextLine();
lineScan = new Scanner(line);
lineScan.useDelimiter(",");
numHits = 0;
numOuts = 0;
numSacrifices = 0;
numWalks = 0;
playerName = lineScan.next();
hit_score_total = 0; //this variable will count the total score so far
while (lineScan.hasNext()) {
action = lineScan.next();
if (action.equals("1") || action.equals("2") || action.equals("3")
|| action.equals("4")){
numHits++;
hit_score_total += Integer.parseInt(action);
}
else if (action.equals("o"))
numOuts++;
else if (action.equals("s"))
numSacrifices++;
//Add code here to check for walks count
else if(action.equals("w"))
numWalks++;
}
//print statistics for the player
System.out.println ("\nStatistics for " + playerName + "... ");
System.out.println ("Hits: " + numHits);
System.out.println ("Outs: " + numOuts);
System.out.println ("Walks: " + numWalks);
System.out.println ("Sacrifices: " + numSacrifices);
// compute total number of at bats:
int at_bats = numHits + numOuts;
//at-bats does not count sacrifices and walks.
// create a DecimalFormat object that limits the number of decimal digits to 3
DecimalFormat df = new DecimalFormat("0.000");
// output the batting average BUT BE CAREFUL
// consider the situation where a batter has no at bats
// for this condition output "No at bats"
// otherwise output "Batting Average: " followed by the average formatted as specified above
if(at_bats == 0){
System.out.println("No at bats.\nNO slugging percentage.");
}
else{
float batting_avg = (float)numHits/at_bats;
System.out.println("Batting Average:"+df.format(batting_avg)); //formating and printing the batting average.
// EXTRA CREDIT: compute slugging percentage and output similar to batting average
float slugging_percentage = (float)hit_score_total/at_bats;
System.out.println("Slugging Percent: "+df.format(slugging_percentage));
}
}
}
}
OUTPUT:
Hope it help!
Get Answers For Free
Most questions answered within 1 hours.