ECOO 2012 Local - Problem #1: Sluggers

The problem:

Two important stats in baseball are the team batting average and the team slugging average. Batting average is defined as the total number of hits (this includes 1 base hits, 2 base hits, 3 base hits and home runs combined) divided by the total number of times at bat("at bats")for all players on the team. The team slugging average is defined using the following equation:

sa = A + 2 × B + 3 × C + 4 × D/E

Where A is the number of 1 base hits, B is 2 base hits, C is 3 base hits, D is home runs, and E is the number of at bats for all players on the team. Both slugging and batting averages are always presented as decimals rounded to 3 places, leaving off the leading 0 (in theory batting averages can be as high as 1.000, and slugging averages as high as 4.000 but in practice they are both usually well below 1).

DATA11.txt (DATA12.txt for the second try) contains the raw data on the top 10 teams during a regular season of Major League Baseball. The first line is the season name, followed by 10 lines for each of the top 10 teams. Each of these lines starts with a team name (single word) followed by 7 integers: Games Played, At Bats, Runs, Hits (total), two-base hits, three-base hits, and home runs. One space character separates each item on each line.

Write a program to produce a report showing the batting and slugging averages for each team in the order they appeared in the input file, and formatted EXACTLY as shown below, including all punctuation and matching upper and lower case exactly. All spacing is done with a single space character. All batting and slugging averages will be less than 1. The final line shows the same averages for all 10 teams combined (computed from the sum of all at bats, hits, etc. for all 10 teams). Note that the two lines of "=" characters each contain 20 characters.

 Sample Input 2011 Regular Season Boston 162 5710 875 1600 352 35 203 NY_Yankees 162 5518 867 1452 267 33 222 Texas 162 5659 855 1599 310 32 210 Detroit 162 5563 787 1540 297 34 169 St.Louis 162 5532 762 1513 308 22 162 Toronto 162 5559 743 1384 285 34 186 Cincinnati 162 5612 735 1438 264 19 183 Colorado 162 5544 735 1429 274 40 163 Arizona 162 5421 731 1357 293 37 172 Kansas_City 162 5672 730 1560 325 41 129 Sample Output 2011 Regular Season ==================== Boston: .280 .461 NY_Yankees: .263 .444 Texas: .283 .460 Detroit: .277 .434 St.Louis: .273 .425 Toronto: .249 .413 Cincinnati: .256 .408 Colorado: .258 .410 Arizona: .250 .413 Kansas_City: .275 .415 ==================== Big 10 Av: .267 .428

My solution (in Java):

```import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.text.DecimalFormat;

public class Problem_1_Sluggers {

public static void main(String[] args) {

try {
DecimalFormat df = new DecimalFormat("#.000"); // to format the output

Scanner scanner = new Scanner(new File("C:\\Users\\Mike\\Desktop\\problem_1_sluggers_DATA10.txt"));
ArrayList<String> list = new ArrayList<String>();

// read in the file to the ArrayList
while (scanner.hasNextLine()) {
}

// print the season name and then remove it from the list
System.out.println(list.get(0) + "\n====================");
list.remove(0);

// sum of each team's averages
double sumOfBatterAverages = 0, sumOfSluggerAverages = 0;

for (String team : list) {

String[] teamStats = team.split("\\s+"); // array of this specific team's stats

System.out.print(teamStats[0] + ": "); // print the team name

int E = Integer.parseInt(teamStats[2]); // at-bats
int H = Integer.parseInt(teamStats[4]); // total hits
int B = Integer.parseInt(teamStats[5]); // total two-base hits
int C = Integer.parseInt(teamStats[6]); // total two-base hits
int D = Integer.parseInt(teamStats[7]); // total two-base hits
int A = H - B - C - D; // total one-base hits is the total hits subtract all the other base hits

// ba --> batter average; sa --> slugger average
double ba = (double)H / E; // number of hits / number of at-bats (remember to parse to a double to avoid integer division)
double sa = (A + 2*B + 3*C + 4*D) / (double)E; // using their formula (remember to parse to a double to avoid integer division)
// one-base hits is multiplied by 1, two base-hits is multiplied by 2, etc.
// each base is worth a point

// add this team's batter/slugger average to the sum of all the batter/slugger averages
sumOfBatterAverages += ba;
sumOfSluggerAverages += sa;

System.out.println(df.format(ba) + " " + df.format(sa)); // print the formatted averages
}

System.out.println("====================");

// 10 teams, so the average of all the teams batter/slugger averages is the sum of them all, divided by 10
double batterAverage = sumOfBatterAverages / 10;
double sluggerAverage = sumOfSluggerAverages / 10;

// print out the formatted total batter/slugger averages of all the teams
System.out.print("Big 10 Av: " + df.format(batterAverage) + " " + df.format(sluggerAverage));

} catch (Exception e) {
System.out.println("Exception");
}

}
}```

My test cases (as .txt file):

Using their sample input:

```2011 Regular Season
Boston 162 5710 875 1600 352 35 203
NY_Yankees 162 5518 867 1452 267 33 222
Texas 162 5659 855 1599 310 32 210
Detroit 162 5563 787 1540 297 34 169
St.Louis 162 5532 762 1513 308 22 162
Toronto 162 5559 743 1384 285 34 186
Cincinnati 162 5612 735 1438 264 19 183
Colorado 162 5544 735 1429 274 40 163
Arizona 162 5421 731 1357 293 37 172
Kansas_City 162 5672 730 1560 325 41 129
```

And the output to that is:

```2011 Regular Season
====================
Boston: .280 .461
NY_Yankees: .263 .444
Texas: .283 .460
Detroit: .277 .434
St.Louis: .273 .425
Toronto: .249 .413
Cincinnati: .256 .408
Arizona: .250 .413
Kansas_City: .275 .415
====================
Big 10 Av: .266 .428
```

Note: The overall batter average for all the teams that I got was .266, however the sample output was .267. There must be some minor floating point inaccuracy which is causing that.

Using their first judging test input:

```2010 Regular Season
NYYankees 162 5567 859 1485 275 32 201
Boston___ 162 5646 818 1511 358 22 211
Tampa_Bay 162 5439 802 1343 295 37 160
Cincinnat 162 5579 790 1515 293 30 188
Texas____ 162 5635 787 1556 268 25 162
Minnesota 162 5568 781 1521 318 41 142
Philadelp 162 5581 772 1451 290 34 166
Colorado_ 162 5530 770 1452 270 54 173
Toronto__ 162 5495 755 1364 319 21 257
ChicagoSo 162 5484 752 1467 263 21 177
```

And the output to that is:

```2010 Regular Season
====================
NYYankees: .267 .436
Boston___: .268 .451
Tampa_Bay: .247 .403
Cincinnat: .272 .436
Texas____: .276 .419
Minnesota: .273 .422
Toronto__: .248 .454
ChicagoSo: .268 .420
====================
Big 10 Av: .264 .428
```

And that is exactly the expected output.

Using their second judging test input:

```2000 Regular Season
ChicagoSo 162 5646 978 1615 325 33 216
Colorado_ 162 5660 968 1664 320 53 161
Cleveland 162 5683 950 1639 310 30 221
Oakland__ 161 5560 947 1501 281 23 239
Houston__ 162 5570 938 1547 289 36 249
SanFranci 162 5519 925 1535 304 44 226
Seattle__ 162 5497 907 1481 300 26 198
St.Louis_ 162 5478 887 1481 259 25 235
KansasCit 162 5709 879 1644 281 27 150
NYYankees 161 5556 871 1541 294 25 205```

And the output to that is:

```2000 Regular Season
====================
ChicagoSo: .286 .470
Cleveland: .288 .470
Oakland__: .270 .458
Houston__: .278 .477
SanFranci: .278 .472
Seattle__: .269 .442
St.Louis_: .270 .455
KansasCit: .288 .425
NYYankees: .277 .450
====================
Big 10 Av: .280 .457
```