简体   繁体   English

计算机科学FRQ

[英]Computer Science FRQ

(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. 进行比赛时,获胜球队(得分较高的球队)将获得2分,而输球队伍将不会获得任何积分。 If there is a tie, both teams get 1 point. 如果平局,两队将获得1分。 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. 每个团队的信息由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
}

The class TeamStandings stores information on the team standings. TeamStandings类存储有关团队排名的信息。 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! 谢谢!

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. 基本上,它只是使获胜团队( curr )处于指定的index参数并增加其分数。 Since the list must be ordered by team points in descending order, just insert the team in their correct position after adjusting the points. 由于列表必须按团队分数降序排列,因此只需在调整分数后将团队插入正确的位置即可。

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

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. 现在,为了按得分值的顺序对排名进行排序,我想该练习希望您做一些将TeamStandings.teamIndex()TeamInfo类中其他方法结合使用的TeamInfo But since the code is either hidden or not written yet, I can't do much more. 但是由于代码是隐藏的或尚未编写,所以我无法做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM