A class testing your code and an example test input file is available for the exercise
student_template_project repository in the directory
Place your code into a file named Standings.java.
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.
Standings should have the following public members. You can design the internal
(private) details freely.
An inner static class
Team(String name): initializes the
Teamobject to represent a team with the given name.
getName()that returns the name of the team.
getPoints(). Each of these returns an
intthat tells the the currently recorded corresponding value for this team.
Teamclass thus needs to store internally at least the number of wins, ties, losses, scored goals, allowed goals and points.
Standings(String filename): the first constructor initializes an initially empty
Standingsobject. The second constructor initializes the
Standingsobject by reading match data from the specified file. The data is read exactly in the same manner as the 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", "3-2", "Finland"]. Furthermore
addMatchResult(String teamNameA, int goalsA, int goalsB, String teamNameB): updates internal match data to contain also the match result descibed by the parameters.
getTeams(): returns an
ArrayList<Team>that lists the teams in the same order as they would be in a standings table (see the
You can return the original internal
Teamobjects (you hopefully have used the
Teamclass 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
Standingsclass as an enclosing class can access private members of
Team. Therefore your
Standingsimplementation can easily modify internal data of
Teamobjects even though outsiders cannot.
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"); standings.printStandings();
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
StandingsTest.java, the example data in the files
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
A+ presents the exercise submission form here.