This course has already ended.

⌛⌛ Standings

A class testing your code and an example test input file is available for the exercise student_template_project repository in the directory Round4/standings. Place your code into a file named

Implement a class Standings that maintains information about teams in a tournament kind of setting: the teams play matches against each other and a team earns 3 points for each win, 1 point for each tie and 0 points for each loss. This corresponds to e.g. the system used in many soccer leagues and tournaments.

The class Standings should have the following public members. You can design the internal (private) details freely.

  • An inner static class Team that has:

    • Constructor Team(String name): initializes the Team object to represent a team with the given name.

    • Member function getName() that returns the name of the team.

    • Member functions getWins(), getTies(), getLosses(), getScored(), getAllowed() and getPoints(). Each of these returns an int that tells the the currently recorded corresponding value for this team.

      • The Team class thus needs to store internally at least the number of wins, ties, losses, scored goals, allowed goals and points.

  • Constructors Standings() and Standings(String filename): the first constructor initializes an initially empty Standings object. The second constructor initializes the Standings object by reading match data from the specified file. The data is read exactly in the same manner as the member function readMatchData (see below).

  • Member function readMatchData(String filename): Reads match data from the specified file and updates internal information accordingly. The match data file is expected to contain lines of form “teamNameA\tgoalsA-goalsB\tteamNameB”. Note that the '\t' are tabulator characters.

    • An example could e.g. be “Iceland\t3-2\tFinland” that describes a match where Iceland scored 3 goals and Finland 2 goals.

    • A hint: the operation "Iceland\t3-2\tFinland".split(\\t) produces a String array ["Iceland", "3-2", "Finland"]. Furthermore "3-2".split("-") gives ["3", "2"].

  • Member function addMatchResult(String teamNameA, int goalsA, int goalsB, String teamNameB): updates internal match data to contain also the match result descibed by the parameters.

  • Member function getTeams(): returns an ArrayList<Team> that lists the teams in the same order as they would be in a standings table (see the printStandings function below).

    • You can return the original internal Team objects (you hopefully have used the Team class for storing internal data…). Returning the original Team objects is safe because the class does not have any public functions that could change data.

    • Also note that the Standings class as an enclosing class can access private members of Team. Therefore your Standings implementation can easily modify internal data of Team objects even though outsiders cannot.

  • Member function printStandings(): prints a formatted standings table that reflects all stored match results.

    • Each row of the standings table gives the team name, number of matches played, wins, ties, losses, goals scored, goals allowed and the points total.

    • The table is sorted according to the following (slightly artificial) rules:

      • Primary rule: points total. Higher points come first.

      • Secondary rule: goal difference (scored minus allowed). Higher difference comes first.

      • Tertiary rule: number of goals scored. Higher number comes first.

      • Last rule: ascending alphabetical order of team names (this rule is artificial).

    • Format the standings table as follows:

      • The team name is padded with trailing spaces, if necessary, so that the number of characters is equal to the longest team name in the group.

      • The numbers of matches, wins, ties and losses are printed using 3 characters of width (padded with leading spaces).

      • The numbers of scored and allowed goals are printed in the form “scored-allowed” using 6 characters of width (padded with leading spaces).

      • The points total is printed using 3 characters of width (padded with leading spaces).

      • Columns are separated by one space character in addition to the widths/padding described above.

The given example output should clarify how the standings table is formed.


You could initially test your Standings class with the following code snippet:

Standings standings = new Standings();
standings.addMatchResult("Tonga", 0, 3, "Cook Islands");
standings.addMatchResult("Samoa", 3, 2, "American Samoa");
standings.addMatchResult("Cook Islands", 1, 0, "Samoa");
standings.addMatchResult("Tonga", 1, 2, "American Samoa");
standings.addMatchResult("Tonga", 0, 3, "Samoa");
standings.addMatchResult("American Samoa", 2, 0, "Cook Islands");

The expected output is shown below. The output is organized fairly nicely into columns due to the widths used when printing each field.

Samoa            3   2   0   1    6-3   6
American Samoa   3   2   0   1    6-4   6
Cook Islands     3   2   0   1    4-2   6
Tonga            3   0   0   3    1-8   0

This is only a small initial test. You can test your code further with the test program given in the file, the example data in the files input.txt and input_b.txt, and the example output in the file output.txt. Place these files and your own class into the same directory, compile the program e.g. as javac *.java, and run the test as java StandingsTest input.txt input_b.txt Mexico 1 1 USA Panama 2 2 Honduras "Costa Rica" 2 1 "Trinidad and Tobago" USA 0 2 "Costa Rica" "Trinidad and Tobago" 1 2 Honduras. The program output should be identical with the example output given in the file output.txt.

A+ presents the exercise submission form here.

Posting submission...