League Match Points

League Match Points

Problem

In a certain sports league, a group of teams plays through a Schedule of Game.  At the end of this Schedule, they want to determine the winner.

To determine the winner, you will have to determine for each team how many Games they won, lost, and tied.  Assuming 2 points for a win, 1 point for a tie, and 0 points for a loss, you can then determine how many points they scored during the season.  The team(s) with the most points is then the winner.

Required Classes (save the class files below to your working directory):

Game.class
Schedule.class

Sample Program Run:

You should build your application to take no input and produce output similar to the following.  If your application works correctly, then your program output should look like the following:

Vancouver – 3 wins, 2 losses, 0 ties = 6 total points
Calgary – 2 wins, 3 losses, 2 ties = 6 total points
Edmonton – 1 wins, 3 losses, 2 ties = 4 total points
Toronto – 2 wins, 3 losses, 2 ties = 6 total points
Ottawa – 4 wins, 3 losses, 1 ties = 9 total points
Montreal – 4 wins, 2 losses, 1 ties = 9 total points

Program Template

public class SeasonWinner{ public static void main (String[] args) {  }} 

Solution 

public class SeasonWinner

{

public static void main(String[] args)

{

final Schedule s = new Schedule();

final String[] teams = s.getTeams();

// to store the statistics of each team

finalint[] wins = new int[teams.length];

finalint[] loses = new int[teams.length];

finalint[] ties = new int[teams.length];

for (int i = 0; i <Schedule.GAMES; i++)

{

final Game g = s.getGame(i);

// find the teams’s index

int idx1 = find(teams, g.getHomeTeam());

int idx2 = find(teams, g.getAwayTeam());

// compare their scores and increase their statistics accordingly

if (g.getHomeScore() >g.getAwayScore())

{

wins[idx1] ++;

loses[idx2] ++;

}

else if (g.getHomeScore() <g.getAwayScore())

{

loses[idx1] ++;

wins[idx2] ++;

}

else

{

ties[idx1] ++;

ties[idx2] ++;

}

}

// calculate the score of each team

finalint[] points = new int[teams.length];

for (int i = 0; i <teams.length; i++)

{

points[i] = 2 * wins[i] + 1 * ties[i] + 0 * loses[i];

}

// display the result

for (int i = 0; i <teams.length; i++)

{

System.out.printf(“%s – %d wins, %d losses, %d ties = %d total points\n”,

teams[i], wins[i], loses[i], ties[i], points[i]);

}

}

// find the index of the team

public static int find(final String[] teams, final String team)

{

for (int i = 0; i <teams.length; i++)

{

if (teams[i].equals(team))

{

return i;

}

}

return -1; // not found, should not happen

}

}