Instructions
Requirements and Specifications
Source Code
/*
* Heralda Lamarre
* Program #1
*/
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class BaseballStatistics {
public static void main(String[] args) throws IOException {
System.out.println("Heralda Lamarre");
System.out.println("Program #1");
int GAMES_IN_SEASON = 25;
String filename = "baseball.txt";
// reading from console or from file depending on command line argument
Scanner scanner = new Scanner(new File(filename));
while(scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split("\\s+");
// checking if user wants to exit
if (parts.length == 1 && Integer.parseInt(parts[0]) == -1) {
// exiting
break;
}
// parsing integers
int id = Integer.parseInt(parts[0]);
int wins = Integer.parseInt(parts[1]);
int losses = Integer.parseInt(parts[2]);
int gamesPlayed = wins + losses;
if (gamesPlayed > GAMES_IN_SEASON) {
System.out.println("Team played more than " + GAMES_IN_SEASON + " games");
continue;
}
int gamesRemaining = GAMES_IN_SEASON - gamesPlayed;
double winningAverage = 0.0;
// calculating average only team played some games
if (gamesPlayed > 0) {
winningAverage = wins * 1.0 / gamesPlayed;
}
// printing simple stats
System.out.println("team " + id);
System.out.println(wins + " wins " + losses + " losses");
System.out.println("total number of games played is " + gamesPlayed + " " + gamesRemaining + " games remaining");
System.out.println("the winning average is " + String.format("%.4f", winningAverage));
// making comparisons
if (gamesRemaining >= wins) {
System.out.println("number of games remaining is greater than or equal to number won");
}
else {
System.out.println("number of games remaining is not greater than or equal to number won");
}
if (gamesRemaining > losses) {
System.out.println("number of games remaining is greater than number lost");
}
else {
System.out.println("number of games remaining is not greater than number lost");
}
}
}
}