0

(EDITED CODE) I am having a bit of an issue I hope I can get some help on. Here are my conditions:

You are developing a program to keep track of team standings in a league. When a game is played, the winning team (the team with the higher score) gets 2 points and the losing team gets no points. If there is a tie, both teams get 1 point. The order of the standings must be adjusted whenever the results of a game between two teams are reported. The following class records the results of one game.

public class GameResult
{
  public String homeTeam()   // name of home team
  { /* code not shown */ }

  public String awayTeam()   // name of away team
  { /* code not shown */ }

  public int homeScore()     // score for home team
  { /* code not shown */ }

  public int awayScore()     // score for away team
  { /* code not shown */ }

  // instance variables, constructors, and other methods not shown
}

The information for each team is stored by an instance of the class TeamInfo whose partial definition is below.

public class TeamInfo
{
  public String teamName()
  { /* code not shown */ }

  public void increasePoints(int points)
  { /* code not shown */ }

  public int points()
  { /* code not shown */ }

  // instance variables, constructors, and other methods not shown
}

The class TeamStandings stores information on the team standings. A partial declaration is shown below.

public class TeamStandings
{
  TeamInfo[] standings; // maintained in decreasing order by points, 
                        // teams with equal points can be in any order

  public void recordGameResult(GameResult result)
  { /* to be completed as part (c) */ }

  private int teamIndex(String name)
  { /* to be completed as part (a) */ }

  private void adjust(int index, int points)
  { /* to be completed as part (B)/> */ }

  // constructors and other methods not shown
}

And here is the actual question:

Write the method adjust. The method adjust should increment the team points for the team found at the index position in standings by the amount given by the parameter points. In addition, the position of the team found at index in standings should be changed to maintain standings in decreasing order by points; teams for which points are equal can appear in any order.

And here is what I have so far:

private void adjust(int index, int points)
{
int Score[] = new int[standings.length]
 for ( int i=0; i < standings.length; i++)
     {
         Score[i] = points;
         Arrays.sort(Score);
}
}

I realize this is very wrong and need a little guidance to solve this. Thank you!

  • yes run it if something else – Benjamin Apr 18 '14 at 14:30
  • Currently, your code simply creates a new `Score` array which has the same size as `standings` and sets every entry to `points`. If you called this method with a `standings` array of size `5` for example and `points` equal to `3`, you'll end up with `[3, 3, 3, 3, 3]` as your `Score` array. After that, you don't do anything with that array, so nothing really happens... – Mattias Buelens Apr 18 '14 at 14:42

3 Answers3

1

Something like this should work:

private void adjust(int index, int points) {
    // increase points of winning team
    TeamInfo curr = standings[index];
    curr.increasePoints(points);
    // get the new score of the winning team
    int points = curr.points();
    // perform an insertion sort on the modified portion
    int i = index;
    while (i > 0 && standings[i-1].points() < points) {
        // shift teams with lower scores rightwards
        standings[i] = standings[i-1];
        i--;
    }
    standings[i] = curr;
}

Basically, it just gets the winning team (curr) at the specified index parameter and increments its points. Since the list must be ordered by team points in descending order, just insert the team in their correct position after adjusting the points.

Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
  • This will work, but I'm afraid that the OP will simply copy-paste this into his homework and won't learn anything. I would recommend the OP to *only* look at the comments in this code snippet and write the code himself. :-) – Mattias Buelens Apr 18 '14 at 14:50
0

problem is :

      for ( int i=0; i <= standings.length; i++)//here index out of bounds 
       {
          Score[i] = index, points;//here 
       }

write like :

     for ( int i=0; i <standings.length; i++)
     {
         Score[i] = points;
     }
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

Here's how to adjust the points for a team in the standings.

private void adjust(int index, int points)
{
  /* 'index' is by definition an index into the standings array
   * 'points' is by definition how many points to give to the team at 'index'
   * each TeamInfo object has a method called increasePoints()
   * therefore, to increase the number of points for a team in the standings... */
  standings[index].increasePoints(points);
}

make sense?

Now, to sort the standings in order of point value, I imagine the exercise wants you to do something that uses TeamStandings.teamIndex() in combination with the other methods in your TeamInfo class. But since the code is either hidden or not written yet, I can't do much more.

Dan O
  • 6,022
  • 2
  • 32
  • 50
  • You won't need `teamIndex` for this, as you already have the index given as value. Only in the implementation of `recordGameResult` will you need both `teamIndex` and `adjust`. – Mattias Buelens Apr 18 '14 at 14:44
  • oops! yeah, that's a getter not a setter. You're right. – Dan O Apr 18 '14 at 14:46