CompSci 6- Classwork 10 - Feb 23, 2010
10 pts

Today's classwork focuses on more practice with classes and ArrayList.

Snarf the 10_chess_cps006_spring10 project to get started.

Here is the code for today.

Problem: Checkmate

Consider the class Player to represent a chess player. Chess is a board game played by two people. A Player stores information about a chess player including their name, their rank (a number that is larger means the player is higher ranked, and a low number below 100 means they are unranked.), and their grade in school, 1-13, with 13 meaning they are out of high school. There is no grade higher than 13.

  1. Complete the constructor for the class Player and all its methods.

  2. Consider the ChessTournament class.

    Here is the code in the main method to test several methods in the class. Note there are some additional lines of code to print pairings for the tournament.

       public static void main(String[] args) throws 
       FileNotFoundException 
       {
          String inputFileName = "chessdata.txt";
          FileReader reader = new FileReader(inputFileName);
          Scanner in = new Scanner(reader);
          ChessTournament Feb8 = new ChessTournament(in);
    
          System.out.println("Grade 5 highest player is: " + Feb8.highestPlayerInGrade(5));
          System.out.println("Grade 9 highest player is: " + Feb8.highestPlayerInGrade(9));
          System.out.println("Grade 4 highest player is: " + Feb8.highestPlayerInGrade(4));
          System.out.println("Number players with rank between 400 and 700 is: "
                + Feb8.numberPlayersBetween(400, 700));
          
          ArrayList highRankPlayers = Feb8.playersWithRankGreater(600);
          System.out.println("Name of players rank greater than 600: ");
          for (String name: highRankPlayers)
          {
             System.out.print(name + " ");
          }      
          System.out.println(" ");
          
          System.out.println(" ");
          System.out.println("Print Pairings");
          System.out.println(" ");
          
          ArrayList players = Feb8.playersInGradeRange(0, 3);
          Feb8.printPairings("Grades K-3 Pairings", players);
          players = Feb8.playersInGradeRange(4, 8);
          Feb8.printPairings("Grades 4-8 Pairings", players);
          players = Feb8.playersInGradeRange(9, 13);
          Feb8.printPairings("Grades 9-13 Pairings", players);      
       }
    
    
    Here is the data file chessdata.txt.
    Narten 680 5
    Lapidus 956 5
    Ward 550 5
    Smith 430 3
    Yee 800 4
    Parker 0 8
    Kumar 1 4
    Guilak 758 5
    Jones 834 9
    Wilson 1012 11
    Gorin 764 2
    Huang 632 1
    Liang 943 10
    Hellinga 843 2
    
    Here is the corresponding output when the program is run with this data file.
    Grade 5 highest player is: Lapidus
    Grade 9 highest player is: Jones
    Grade 4 highest player is: Yee
    Number players with rank between 400 and 700 is: 4
    Name of players rank greater than 600: 
    Narten Lapidus Yee Guilak Jones Wilson Gorin Huang Liang Hellinga  
    Print Pairings
     
    Grades K-3 Pairings
    ========
    Smith Hellinga
    Huang Gorin
     
    Grades 4-8 Pairings
    ========
    Parker Lapidus
    Kumar Yee
    Ward Guilak
    Narten BYE
     
    Grades 9-13 Pairings
    ========
    Jones Wilson
    Liang BYE
    

    Note that the Scanner input is already bound to a file in main. The file is in the following format. Each line in the file has the name of a player (containing no blanks), the rank of the player as an integer and the grade of a player as an integer.

    Complete the constructor for the ChessTournament. (hint: what do you need to construct with new?)

  3. Complete the method numberPlayersBetween that has two parameters minRank and maxRank and returns the number of players whose rank is between minRank and maxRank inclusive.

  4. Complete the method highestPlayerInGrade that has a grade as a parameter and returns the name of the highest ranking player in that grade, or returns the string ``No Player in this grade'' if there are no players in that grade.

  5. Complete the method playersWithRankGreater that has one parameter, a rank, and returns an ArrayList of names of players with that rank.

  6. Complete the method getNameWithRank that has one parameter, a rank, and returns the name of the player with that rank. Assume all ranks are unique.

  7. Complete the method playersInGradeRange that has two integer parameters lowgrade and highgrade. It returns an array of Players whose grade is between lowgrade and highgrade inclusive.

  8. Complete the method rankOfPlayers which has one parameter, an ArrayList of Player types and returns an ArrayList of the ranks of these players in sorted order from lowest to highest.

    Hint: Use Collections.sort(NAMEOFARRAYLIST) to sort an ArrayList of type Integer.

  9. Complete the method printPairings that has two parameters, a title to print and an ArrayList of Players. Given a list of Players you would print the pairs of players who would play each other. The highest rank player plays the lowest rank player, the next highest rank player plays the next lowest rank player. If there is an odd number of players, the player whose rank is in the middle gets a BYE. Print their name with the word BYE.

Submit

You should test your program out with another data file you create.

When you are satisfied that you have completed the problem above, you should submit your project through Eclipse using assignment name Class10-Feb23. A submission is not considered complete unless it includes all the Java code for the project (both what you have written and the code provided when you downloaded the project) and a README file.