2016-12-06 46 views
1

我刚刚在5周前开始学习计算机科学(Java),并且仍然无法创建方法。我被分配创建一个NFL统计类,然后创建一个方法来显示计算。一切顺利,直到我在测试课上打电话给我的方法。这里似乎缺少什么?如何在另一课中测试我的方法?

NFLPlayer CLASS(Containin方法):

private int touchdowns; 
private int interceptions; 
private int passingAttempts; 
private int completedPasses; 
private int passingYards; 
private int runningYards; 
private int recievingYards; 
private int tackles; 
private int sacks; 


// Method for Quarterback rating 
public double QBRating(int touchdowns, int passingAttempts, int completedPasses, 
     int passingYards, int interceptions) { 

     double a = (completedPasses/passingAttempts - 0.3) * 5; 
     double b = (passingYards/passingAttempts - 3) * 0.25; 
     double c = (touchdowns/passingAttempts) * 25; 
     double d = 2.375 - (interceptions/passingAttempts * 25); 
     double ratingQB = ((a + b + c + d)/6) * 100; 
     { 
     return ratingQB; 
     } 

} 

现在这里是我有显示我的计算麻烦我的测试类

class MyTest { 
public static void main(String[] args) { 

    NFLPlayer playerStats = new NFLPlayer(); 

    //Player1 finding quarterback rating 
    int touchdowns = 2; 
    int passingAttempts = 44; 
    int passingYards = 285; 
    int interceptions = 1; 
    int completedPasses = 35; 

    // Call QB rating method 
    playerStats.QBRating(touchdowns, passingAttempts, completedPasses, 
      passingYards, interceptions); 

    System.out.println(QBRating); 
} 

}

+1

你QBRating方法返回双仅仅收取结果双,然后用系统输出 –

+0

你是说@ShekharKhairnar打印的内容我我正在做的只是收集结果,或者我需要收集结果? –

+1

您需要收集我在评论中提到的结果,例如double result =你调用QBRating方法的代码 –

回答

3

而不是通过这么多int参数(易混合起来)你的方法,你可以给你的NFLPlayer类私有字段为每个值:

public class NFLPlayer { 

    private final String name; 

    private int touchdowns; 
    private int passingAttempts; 
    private int completedPasses; 
    private int passingYards; 
    private int interceptions; 

    public NFLPlayer(String name) { 
     this.name = name; 
    } 

    // Method names start with a lower case character in Java 
    // The name should usually be an imperative 'do something' not a noun ('something') 
    // although there are exceptions to this rule (for instance in fluent APIs) 
    public double calculateQbRating() {     
     double a = (completedPasses/passingAttempts - 0.3) * 5.0; 
     double b = (passingYards/passingAttempts - 3.0) * 0.25;    
     // an AritmeticException will occur if passingAttempts is zero 
     double c = (touchdowns/passingAttempts) * 25.0; 
     double d = 2.375 - (interceptions/passingAttempts * 25.0); 
     return ((a + b + c + d)/6.0) * 100.0; 
    }  

    public String getName() { 
     return name; 
    } 

    // setter for the touchdowns field 
    public void setTouchdowns(int value) { 
     touchdowns = value; 
    } 

    // TODO: add other setters for each private field 

    @Override 
    public String toString() { 
     return String.format("Player %s has QB rating %s", name, calculateQbRating()); 
    } 
} 

您的应用程序(这不叫测试):

class NFLApplication { 

    public static void main(String[] args) {  

      NFLPlayer playerStats = new NFLPlayer("johnson");  

      playerStats.setTouchdowns(2); 
      playerStats.setPassingAttempts(44); 
      playerStats.setPassingYards(285); 
      playerStats.setInterceptions(1); 
      playerStats.setCompletedPasses(35); 

      double qbRating = playerStats.calculateQbRating();  

      System.out.println(qbRating); 
     } 
} 

一种使用JUnit框架您NFLPlayer类测试(JUnit的通常是你的IDE中包含默认情况下):

public class NFLPlayerTest { 

    // instance of the class-under-test 
    private NFLPlayer instance; 

    // set up method executed before each test case is run 
    @Before 
    public void setUp() { 
     instance = new NFLPlayer(); 
    } 

    @Test 
    public void testCalculateQbRatingHappy() { 
     // SETUP 
     instance.setTouchdowns(2); 
     instance.setPassingAttempts(44); 
     instance.setPassingYards(285); 
     instance.setInterceptions(1); 
     instance.setCompletedPasses(35); 

     // CALL 
     double result = playerStats.calculateQbRating(); 

     // VERIFY 
     // assuming here the correct result is 42.41, I really don't know 
     assertEquals(42.41, result); 
    } 

    @Test 
    public void testCalculateQbRatingZeroPassingAttempts() { 
     // SETUP 
     // passingAttempts=0 is not handled gracefully by your logic (it causes an ArithmeticException) 
     // you will probably want to fix this 
     instance.setPassingAttempts(0); 

     // CALL 
     double result = playerStats.calculateQbRating(); 

     // VERIFY 
     // assuming here that you will return 0 when passingAttempts=0 
     assertEquals(0, result); 
    } 
} 

这个测试类应该在您的测试源目录(通常在yourproject/src/test/yourpackage/)。它需要一些导入,因为JUnit默认情况下通常可用,所以应该可以在IDE中轻松解决。

要运行测试,请右键单击它,并根据您使用的IDE(IDE是Eclipse,NetBeans或IntelliJ等开发工具)选择“运行测试”,“测试文件”等类似的东西。 。您应该看到一些测试输出,指示测试是否成功(绿色)或是否有故障(红色)。进行这样的测试非常有用,因为它会强制您考虑设计并编写更好的代码。 (可测试的代码通常比难以测试的代码好),并且因为新的更改会导致现有代码中的错误(回归),它会警告您。

编辑:

要创建两个球员,你必须创建两个实例使用不同的统计(我加了一个name场,所以我们可以更容易地分辨出玩家):

NFLPlayer player1 = new NFLPlayer("adams"); 
NFLPlayer player2 = new NFLPlayer("jones"); 

,并给他们每个自己的统计数据:

player1.setTouchdowns(2); 
player1.setPassingAttempts(4); 
player1.setPassingYards(6); 
player1.setInterceptions(8); 
player1.setCompletedPasses(10); 

player2.setTouchdowns(1); 
player2.setPassingAttempts(3); 
player2.setPassingYards(5); 
player2.setInterceptions(7); 
player2.setCompletedPasses(9); 

你甚至可以创造的球员名单:

List<NFLPlayer> players = new ArrayList<>(); 
players.add(player1); 
players.add(player2); 

然后你可以例如打印出所有球员的评分在一个循环:

for(NFLPlayer player : players) { 
    // this uses the `toString` method I added in NFLPlayer 
    System.out.println(player); 
} 
+0

由于我在网上做了所有事情,这有助于我更好地了解如何设置一个完整的程序。我正在创建一个框架,最终将成为多个不同统计数据的玩家。通过使用“set.Touchdowns(2);”等。这只是一个测试?还是我仍然可以为不同的球员应用不同的统计数据 –

+0

'player1.setTouchdowns(2)'将player1的达阵设置为2.您可以为不同的玩家设置不同的值,也可以更改玩家的值。 –

0

你不应该叫方法名称在SOP内而不是System.out.println(playerStats.QBRating(达阵,passingAttempts,completedPasses, passingYards,拦截)); 或在您的类中重写toString()方法并将方法调用分配给本地变量并打印该值。 还可以使用一些框架(Junit的),而不是写存根

+0

感谢您的快速反馈。我对这一切都很陌生,所以非常感谢。什么是(Junit)框架?当你说重写字符串,你的意思是在我的NFLPlayer类中创建一个字符串方法? –

相关问题