In: Computer Science
SLUGGING PERCENTAGES & BATTING AVERAGES - SENTINEL LOOP
Develop a program in JAVA that will determine the slugging percentages and batting average of several New York Yankees from the 2006 season. Slugging percentage is calculated by dividing the total number of bases by the number of at bats. The number of bases would be one for every single, two for every double, three for every triple, and four for every home run. The batting average is calculated by dividing the total number of hits by the number of at bats. You do not know the number of players in advance, but for each player you know their number of singles, doubles, triples, home runs, total number of at bats, and the player's name (use the proper type and method for each variable).
You will use a while sentinel loop on the 1st item to input. If it is not the sentinel, go into the while sentinel loop and separately input the rest of the input items (be careful of the enter in the input memory buffer for the player name). You will then calculate the total bases and the slugging percentage. You will then calculate the batting average. You will then print out the player's name, the labeled slugging percentage for that player to three decimal places, and the labeled batting average for that player to three decimal places. Use separate output statements. Print a blank line between players. This loop will repeat for as many players as you need.
Run your program with the following players and sentinel value (to show the sentinel value worked):
Singles: 158
Doubles: 39
Triples: 3
Home Runs: 14
At Bats: 623
Player: Derek Jeter
Singles: 51
Doubles: 25
Triples: 0
Home Runs: 37
At Bats: 446
Player: Jason Giambi
Singles: 104
Doubles: 26
Triples: 1
Home Runs: 35
At Bats: 572
Player: Alex Rodriguez
Singles: -1
Solution: Things to remember:
import java.util.*;
import java.lang.*;
import java.io.*;
class Baseball
{
public static void main (String[] args) throws java.lang.Exception
{
int singles=-1, doubles=0, triples=0, homerun=0, bats=0;
String name="";
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String[] s = input.split(":", 2);
try {
singles = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
while(singles != -1){
input = in.nextLine();
s = input.split(":", 2);
try {
doubles = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
input = in.nextLine();
s = input.split(":", 2);
try {
triples = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
input = in.nextLine();
s = input.split(":", 2);
try {
homerun = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
input = in.nextLine();
s = input.split(":", 2);
try {
bats = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
input = in.nextLine();
s = input.split(":", 2);
name = s[1];
int bases = 0, hits = 0;
bases = 1*singles + 2*doubles + 3*triples + 4*homerun;
hits = singles + doubles + triples + homerun;
float slug = (float)bases/bats;
float bat_avg = (float)hits/bats;
System.out.println("Name : " + name);
System.out.println("Slugging percentage : " + String.format("%.3f", slug));
System.out.println("Batting average : " + String.format("%.3f", bat_avg));
System.out.println();
input = in.nextLine();
s = input.split(":", 2);
try {
singles = Integer.parseInt(s[1].trim());
}
catch (NumberFormatException e) {
}
}
}
}
Input format:
Singles: 158 Doubles: 39 Triples: 3 Home Runs: 14 At Bats: 623 Player: Derek Jeter Singles: 51 Doubles: 25 Triples: 0 Home Runs: 37 At Bats: 446 Player: Jason Giambi Singles: 104 Doubles: 26 Triples: 1 Home Runs: 35 At Bats: 572 Player: Alex Rodriguez Singles: -1 |
Output:
Name : Derek Jeter Name : Jason Giambi Name : Alex Rodriguez |
PS: Let me know if you face any issue to run the code or have any doubts.