计算机科学FRQ

时间:2014-04-18 14:15:32

标签: java arrays

(编辑代码) 我有一个问题,希望我能得到一些帮助。这是我的条件:

  

您正在开发一个程序来跟踪联盟中的球队积分。当比赛进行时,获胜球队(得分较高的球队)获得2分,失败球队获得积分。如果有平局,两队都得到1分。每当报告两支球队之间的比赛结果时,必须调整积分的顺序。以下课程记录一场比赛的结果。

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
}
  

每个团队的信息由TeamInfo类的实例存储,其部分定义如下。

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
}
  

班级TeamStandings存储有关团队排名的信息。部分声明如下所示。

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
}

以下是实际问题:

  

编写方法调整。方法adjust应该增加在积分中索引位置找到的团队的团队积分,增加参数点给出的数量。此外,应该改变在积分榜中找到的球队的位置,以便按积分递减排名;积分相等的团队可以按任何顺序出现。

这是我到目前为止所做的:

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);
}
}

我意识到这是非常错误的,需要一点指导来解决这个问题。谢谢!

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

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;
}

基本上,它只是让获胜团队(curr)获得指定的index参数并递增其分数。由于列表必须按降序排列,因此只需在调整点后将团队插入正确的位置。

答案 1 :(得分:0)

问题是:

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

写得像:

     for ( int i=0; i <standings.length; i++)
     {
         Score[i] = points;
     }

答案 2 :(得分:0)

以下是如何调整积分榜中队伍的积分。

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);
}

有意义吗?

现在,为了按照点值的顺序对排名进行排序,我想这个练习要求你做一些将TeamStandings.teamIndex()TeamInfo类中的其他方法结合使用的内容。但由于代码隐藏或尚未编写,我无法做更多。