2016-11-06 5 views
0

私はトーナメントのクラスを持っています。私は、最も低い得点を得て、それから作曲家を作って勝者を得ようとします。 winnerScoreメソッドを私の勝者メソッドに使うことができると思ったのですか? スコアの最も低い名前の作曲家を作成する

この

は私の試みです: (しかし、彼らは同じタイプではありませんので、私はエラーで終わる)

/** 
* Returns the list of winners, that is, the names of those players 
* with the lowest total score. 
* The winners' names should be stored in the same order as they occur 
* in the tournament list. 
* If there are no players, return empty list. 
* @return list of winners' names 
*/ 
public ArrayList<String> winners() { 
    ArrayList<String> result = new ArrayList<String>(); 

    if (result.isEmpty()) { 
     return null; 
    } 

    result.add(players); 

    // Supply this code! 
    return result; 
} 

私は私が作ったこの方法を持って、それをincoperateするためにいくつかの方法があります勝者の方法に?

/* 
* Assume as precondition that the list of players is not empty. 
* Returns the winning score, that is, the lowest total score. 
* @return winning score 
*/ 
public int winningScore() { 
    Player thePlayer = players.get(0); 
    int result = thePlayer.totalScore(); 
    // Supply this code! 
    for(int i=0; i <par.length; i++) 
     if(par[i] > result) 
      result = par[i]; 

    return result; 
} 

これは勝者方法のJUnitテストです:

@Test(timeout=3000) 
public void testWinners() { 
    int [] par = {3,4,5,4,5,3,4,3,5,3,4,5,4,3,4,5,4,3}; 
    int [] scores1 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4}; 
    int [] scores2 = {4,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4}; 
    int [] scores3 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,5}; 
    Tournament T = new Tournament(par); 
    T.enter("Norman", 2, scores1); 
    T.enter("Palmer", 4, scores2); 
    T.enter("Scott", 1, scores3); 
    ArrayList<String> winners = T.winners(); 
    assertTrue(winners.get(0).equals("Norman")); 
} 

すべてのヘルプは非常に感謝を理解されるであろう。

+1

あなたはこの空のリストが空であるならば、あなたがテストし、空のリストを作成することによって開始し、O場合、nullを返します。だからあなたは常にnullを返します。そして、あなたは今までではいけません。メソッドのjavadocを返します。 –

+0

あなたのwinnngスコアは、あなたが思っていることを確信していますか? –

答えて

0

私はこのままではいけません。改善点:

@Test(timeout=3000) 
public void testWinners() { 
    Tournament t = new Tournament(); 
    // int [] par = {3,4,5,4,5,3,4,3,5,3,4,5,4,3,4,5,4,3}; // par does not matter 
    int [] scores1 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4}; 
    int [] scores2 = {4,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,4}; 
    int [] scores3 = {3,4,3,5,3,4,4,3,5,3,3,4,3,4,3,4,3,5}; 
    t.enter("Norman", 2, scores1); 
    t.enter("Palmer", 4, scores2); 
    t.enter("Scott", 1, scores3); 
    assertTrue(winners.get(0).equals("Palmer")); 
} 

とクラス:

public class Tournament { 

    List<Player> players = new ArrayList<>(); 

    private void enter(String name, int par, int[] scores) { 
    players.add(new Player(name, par, scores)); 
    } 

    public List<Player> getWinners() { 
    List<Player> ps = new ArrayList<Player>(players); 
    Collections.sort(ps); 
    return ps; 
    } 

    private class Player implements Comparable<Player> { 

    public String name; 
    public int totalScore; 

    public Player(String name, int par, int[] scores) { 
     this.name = name; 
     for (int score : scores) { 
      totalScore += score; 
     } 
     //System.out.println(" " + name + " " + totalScore + ", par " + par + ", total " + (totalScore - par)); 
     totalScore -= par; 
    } 

    @Override 
    public int compareTo(Player o) { 
     return Integer.compare(totalScore, o.totalScore); 
    } 

    } 

} 
関連する問題