Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
//File: BaseballTeam.java //Purpose: Encapsulates information about a single baseball team // home (city or state) // name (team name) // league(either AL (American league) or NL (national league) // division (each league has three divisions C(entral), E(ast), W(est) // wins - the number of wins // losses - the number of losses // percentage - the percentage of games played that are wins //You must complete the methods that are highlight with **** import java.text.DecimalFormat; public class BaseballTeam { //Instance variables private String home; private String name; private String league; private String division; private int wins, losses; private double percentage; //**** //**** //**** public { } Complete the Constructor The six parameters initialize the first six instance variables percentage must be calculated. BaseballTeam(String h, String n, String lg, String d, int w, int lo) //Accessor methods public String getHome() public String getName() public String getDivision() public String getLeague() public int getWins() public int getLosses() public double getPercentage() { { { { { { { return return return return return return return home; } name; } division; } league; } wins; } losses; } percentage; } //returns a String representation of this baseball team public String toString() { return String.format("%-3s%-2s%-15s%13s: %5d %5d %8.3f", league, division, home, name, wins, losses, percentage); } // compare BaseBallTeams by league and then percentage public int compareToLP(BaseballTeam other) { int compare = this.league.compareTo(other.league); if(compare == 0) { int thisPercentage = (int)(1000 * this.percentage); int otherPercentage = (int)(1000 * other.percentage); compare = otherPercentage - thisPercentage; } return compare; } //**** Add the method that compares BaseBallTeams first by league, //**** then by division and finally by percentage.(see above) public int compareToLDP(BaseballTeam other) { return 0; } } /* File: BaseballTeamResuts.java * Lab 12 * Purpose: * * * */ Reads baseball teams and their win/loss records from a file. Then sorts and prints teams by percentages first by league and then by league and division. You must complete the code highlight with **** import java.io.*; import java.util.*; //File //Scanner public class BaseballTeamResults { public final int NUMBER_OF_TEAMS = 30; BaseballTeam[] teams = new BaseballTeam[NUMBER_OF_TEAMS]; Scanner keyboard = new Scanner(System.in); //Scanner around System.in Scanner fileScanner = null; //Scanner around a file PrintStream outfile = System.out; //PrintStream may be changed to file public static void main(String[] args) { BaseballTeamResults btr = new BaseballTeamResults(); btr.run(); } public void run() { openFiles(); //fillArray(); //outfile.println("\n\nPrinting all teams as entered from file\n"); //printArray(); //sortLP(teams); //outfile.println( "\n\nPrinting all teams sorted by League - Percentage\n"); //printArrayLP(); //sortByLDP() //outfile.println( "\n\nPrinting all teams sorted by League - Division - Percentage\n"); //printArrayLDP(); //outfile.println("\n\nPrinting teams in playoffs\n"); //printPlayOffTeams(); } //Create the Scanner fileS object and //possibly create a new PrintStream outfile private void openFiles() { System.out.print("Enter the name of the input file: "); String fileName = keyboard.nextLine(); do { try { fileScanner = new Scanner(new File(fileName)); System.out.print( "\nCurrently output is directed to the monitor." + "\nIf you would prefer that output be directed to a file," + "\nenter the name of the File. Otherwise hit <Enter>: "); fileName = keyboard.nextLine(); if(!(fileName.equals(""))) outfile = new PrintStream(fileName); } catch(FileNotFoundException e) { System.out.print(fileName + " could not be opened." + "\nRe-enter filename or <Enter> to quit: "); fileName = keyboard.nextLine(); if(fileName.equals("")) System.exit(0); } }while(fileScanner == null); } //**** Complete this method so that it is robust **** //**** Comments are in the method **** public void fillArray() { int lineCounter = 0; //is incremented when a line is read in int index = 0; //index into the array teams int errorCount = 0; //is incremented when an exception is caught while(fileScanner.hasNextLine()) { //get the nextLine and increment lineCounter //Create the line Scanner and useDelimiter(":") //try //extract the information (4 strings and 2 ints //representing home, name, league, division, //wins, losses) from the line scanner //create a BaseballTeam object, adding it to the array. //catch a generic Exception. //Print the exception and the line that has the error //increment the error count } //if the error count is greater than 0, //print an appropriate message and end the program. } // Print the array - one team per line public void printArray() { for(int j = 0; j < teams.length; j++) outfile.println(teams[j]); } //**** //**** //**** public { } Sort the teams by League - Division - Percentage using the selection sort algorithm (see below) Uses the compareToLDP method in BaseballTeam class void sortLDP(BaseballTeam[] ar) //***Prints the elements in the array after they //***have been sorted by using the sortLP method.(See below) public void printArrayLDP() { } //Sort the teams by League and then Percentage //using the selection sort algorithm //Uses the compareToLP method in BaseballTeam class public void sortLP(BaseballTeam[] ar) { for(int j = 0; j < ar.length-1; j++) { int indexOfMin = j; for(int k = j + 1; k < ar.length; k++) { if(ar[k].compareToLP(ar[indexOfMin])<0) { indexOfMin = k; } } if( j != indexOfMin) { BaseballTeam temp = ar[j]; ar[j] = ar[indexOfMin]; ar[indexOfMin] = temp; } } } //Prints the elements in the array after they have been sorted //by using the sortLP method. public void printArrayLP() { outfile.println("\n AMERICAN LEAGUE TEAMS \n"); int index = 0; while(teams[index].getLeague().equals("AL")) outfile.println(teams[index++]); outfile.println("\n NATIONAL LEAGUE TEAMS \n"); while(index < teams.length) outfile.println(teams[index++]); } }