2016-11-06 5 views
0

나는 가장 낮은 점수를 얻고 arraylist를 만들어 승자를 얻으려고하는 토너먼트 강습을하고 있습니다. 내 winnersScore 메서드를 승자 메서드에 사용할 수있을 것 같습니까? 가장 낮은 점수를 가진 이름의 arraylist 만들기

내 시도 : (하지만 그들은 동일한 유형하지 않기 때문에 나는 오류와 끝까지)

/** 
* 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 몇 가지 방법이 승자 방법으로?

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

어떤 도움이 크게 감사합니다 감사 :

/* 
* 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 테스트입니다.

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

    } 

} 
관련 문제