/************************************************ * Donna Gabai, dgabai, P01A, spring12 * * Compile: javac NBAStats.java * Execute: java NBAStats < 5NBA.txt * Dependencies: StdIn, StdOut * * Computes basketball player stats from input given. * Outputs leaders in points, points per game and points per 48 minutes. * Outputs pairs of players with comparable points per game * ************************************************************************/ public class NBAStats { // compute per game stats for one player public static double perGame(int total, int games) { return (double) total / games; } //compute points per 48 minutes for a player public static double per48Min(double ppg, double mpg) { return (ppg / mpg) * 48; } // find index for the player with highest stat (no ties) public static int findBest(int[] stat) { int N = stat.length; // since the stats must be >=0, safe to assume max is at least 0 int max = -1; int index = -1; for (int i = 0; i < N; i++) { if (stat[i] > max) { max = stat[i]; index = i; } } return index; } // find index for the player with highest stat (no ties) public static int findBest(double[] stat) { int N = stat.length; // since the stats must be >=0, safe to assume max is at least 0 double max = -1.0; int index = -1; for (int i = 0; i < N; i++) { if (stat[i] > max) { max = stat[i]; index = i; } } return index; } public static void main(String[] args) { // read in stats from standard input int N = StdIn.readInt(); // set up arrays for player data String[] first = new String[N]; String[] last = new String[N]; int[] gamesPlayed = new int[N]; int[] points = new int[N]; int[] minutesPlayed = new int[N]; // input player data for (int i = 0; i < N; i++) { first[i] = StdIn.readString(); last[i] = StdIn.readString(); gamesPlayed[i] = StdIn.readInt(); points[i] = StdIn.readInt(); minutesPlayed[i] = StdIn.readInt(); } // compute arrays for points per game, minutes per game, // points per 48 minutes double[] ppg = new double[N]; double[] mpg = new double[N]; double[] ptsPer48 = new double[N]; for (int i = 0; i < N; i++) { ppg[i] = perGame(points[i], gamesPlayed[i]); mpg[i] = perGame(minutesPlayed[i], gamesPlayed[i]); ptsPer48[i] = per48Min(ppg[i], mpg[i]); } // output leaders in all three categories int pointLeader = findBest(points); StdOut.println(first[pointLeader] + " " + last[pointLeader] + " leads the NBA with " + points[pointLeader] + " points."); int ptPerGameLeader = findBest(ppg); StdOut.println(first[ptPerGameLeader] + " " + last[ptPerGameLeader] + " leads the NBA with " + ppg[ptPerGameLeader] + " points per game."); int ptPer48Leader = findBest(ptsPer48); StdOut.println(first[ptPer48Leader] + " " + last[ptPer48Leader] + " leads the NBA with " + ptsPer48[ptPer48Leader] + " points per 48 minutes."); // output comparable players based on points per game for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (Math.abs(ppg[i] - ppg[j]) < 1) { StdOut.println(first[i] + " " + last[i] + " and " + first[j] + " " + last[j] + " have comparable points per game."); } } } } }