import java.io.*; import java.util.*; import java.text.*; /**************************************************************************** * Project 2 for CS 1054 Fall 2003 * Quarterback Rating Calculator * * File: testQBRating.java * Programmer: Mir Farooq Ali * Java Platform: JDK 1.4.2_01 * Compiler: BlueJ - Version 1.3.0 * Last modified: October 1, 2003 * * Purpose: This program takes input from a file named "qbstats.txt", * which contains ten lines, each with the following information delimited * with the | character: * - Quarterback name * - Yards * - Attemps * - Completions * - Touchdowns * - Interceptions * The program prints out the quarterback ratings for each player by * invoking methods from the QuarterbackRating class. ****************************************************************************/ public class testQBrating { public static void main(String[] args) throws Exception { // String objects holding the input and output filenames String myInFileName = "qbstats.txt"; String myOutFileName = "qbratings.txt"; //********************************************************************** //* SET UP THE BUFFERED INPUT STREAM READER * //********************************************************************** // if input is from the *keyboard* use this line...... // myInput = new BufferedReader(new InputStreamReader(System.in) ); // // ....but if input is from a *file* use these lines FileInputStream myFileIS = new FileInputStream(new File(myInFileName) ); BufferedReader myInput = new BufferedReader(new InputStreamReader(myFileIS) ); //********************************************************************** //********************************************************************** //* SET UP THE PRINT STREAM / OUTPUT STREAM * //********************************************************************** // if output if to the *screen* us this line...... // myOutput = System.out; // // ....but if output is to a *file* use these lines FileOutputStream myFileOS=new FileOutputStream(new File(myOutFileName)); PrintStream myOutput = new PrintStream(myFileOS); //********************************************************************** String qbName; // Quarterback name int A; // Number of attempts int C; // Number of completions int Y; // Yards int T; // Number of touchdowns int I; // Number of completions myOutput.println("Mir Farooq Ali"); myOutput.println("CS 1054: Project 2 Fall 2003"); myOutput.println(); myOutput.println("Name\t\tRating\tComp%\tAvgYds\tTD%\tInt%"); myOutput.println("========================================================="); /** This loop is used to extract the first five quarterback names * and statistics from the input file and prints out their quarterback * ratings by invoking the getQBratings() method from the * QuarterbackRating() class */ for (int i = 0; i < 5; i++) { String qbstats; // String to stats line from input file qbstats = myInput.readLine(); StringTokenizer st; // Streamtokenizer to break up input line into tokens st = new StringTokenizer(qbstats, "\\|"); qbName = st.nextToken(); // Extract name Y = Integer.parseInt(st.nextToken()); // Extract yards values A = Integer.parseInt(st.nextToken()); // Extract # of attempts C = Integer.parseInt(st.nextToken()); // Extract # of completions T = Integer.parseInt(st.nextToken()); // Extract # of tds I = Integer.parseInt(st.nextToken()); // Extract # of interceptions // Create a new QuarterbackRating object QuarterbackRating qb = new QuarterbackRating(A, C, Y, T, I); // Invoke the getQBRating method to get the rating float qbRating = qb.getQBRating(); //Set up formatting for the output numbers DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(1); df.setMinimumFractionDigits(1); String qbRatingString = df.format((double)qbRating); // Print out the Quarterback name and his rating myOutput.print(qbName + "\t" + qbRatingString); //Set up formatting for the output numbers df.setMaximumFractionDigits(3); df.setMinimumFractionDigits(3); // Print out the individual components of the QB rating myOutput.print("\t" + df.format((double)(qb.calculateCompletionPercentage()))); myOutput.print("\t" + df.format((double)qb.calculateAverageYards())); myOutput.print("\t" + df.format((double)qb.calculateTDPercentage())); myOutput.println("\t" + df.format((double)qb.calculateIntPercentage())); } /** This loop is used to extract the last five quarterback names * and statistics from the input file and prints out their quarterback * ratings by invoking the getQBratings() method from the * QuarterbackRating() class. The difference from the first loop is * how the QuarterbackRating object is created by using a different * constructor and then using the set methods. * */ for (int i = 0; i < 5; i++) { String qbstats; // String to stats line from input file qbstats = myInput.readLine(); StringTokenizer st; // Streamtokenizer to break up input line into tokens st = new StringTokenizer(qbstats, "\\|"); qbName = st.nextToken(); // Extract name Y = Integer.parseInt(st.nextToken()); // Extract yards values A = Integer.parseInt(st.nextToken()); // Extract # of attempts C = Integer.parseInt(st.nextToken()); // Extract # of completions T = Integer.parseInt(st.nextToken()); // Extract # of tds I = Integer.parseInt(st.nextToken()); // Extract # of interceptions // Create a new QuarterbackRating object QuarterbackRating qb = new QuarterbackRating(); // Set the properties for the quarterback qb.setAtt(A); qb.setComp(C); qb.setYards(Y); qb.setTDs(T); qb.setInts(I); //Set up formatting for the output numbers DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(1); df.setMinimumFractionDigits(1); float qbRating = qb.getQBRating(); String qbRatingString = df.format((double)qbRating); // Print out the Quarterback name and his rating myOutput.print(qbName + "\t" + qbRatingString); //Set up formatting for the output numbers df.setMaximumFractionDigits(3); df.setMinimumFractionDigits(3); // Print out the individual components of the QB rating myOutput.print("\t" + df.format((double)(qb.calculateCompletionPercentage()))); myOutput.print("\t" + df.format((double)qb.calculateAverageYards())); myOutput.print("\t" + df.format((double)qb.calculateTDPercentage())); myOutput.println("\t" + df.format((double)qb.calculateIntPercentage())); } } }