2012-09-24 24 views
0

日期复活节我写了一个名为复活节类(见下文),并命名为EasterTester(也低于)一个测试类来执行它,并插上年份值。目标是实现高斯的算法,用于计算任意指定年份复活节周日的月份和日期。在Java和UML方法语法

我的代码工作正常,但我有我的书有点混乱,我下面。它告诉我不要在我的第一个代码链接的底部实现两个getter方法,因为我“不需要它们”。正如我的代码,我肯定需要它们。有什么我失踪涉及?

此外,它列出了我建议作为使用“复活节类公共接口”:

calculateEaster(INT aYear):字符串

“的UML方法语法指示该方法将一个整数参数并返回一个字符串。“我不明白这个评论,因此,我不明白如何编辑我的代码以遵循这些指导方针。

如果任何人都可以提供对困境/问题的任何清晰,我会非常赞赏!

代码: /** * * @author B_T * */

类复活节{

/** 
* @param n is the month 
* @param p is the day 
*/ 
private int n; 
private int p; 

// Comments via Cay Horstmann's "Big Java" 4th ed. on page 169; p.4.19 

// Let y be the year (such as 1800 or 2001). 

/** 
* 
* @param y this will hold the year that users enter 
*/ 
Easter(int y) { 

    // Divide y by 19 and call the remainder a. Ignore the quotient. 
    int a = y % 19; 

    // Divide y by 100 to get a quotient b and a remainder c. 
    int b = y/100; 
    int c = y % 100; 

    // Divide b by 4 to get a quotient d and a remainder e. 
    int d = b/4; 
    int e = b % 4; 

    // Divide 8 * b + 13 by 25 to get a quotient g. Ignore the remainder. 
    int g = (8 * b + 13)/25; 

    // Divide 19 * a + b - d - g + 15 by 30 to get a remainder h. Ignore the quotient. 
    int h = (19 * a + b - d - g + 15) % 30; 

    // Divide c by 4 to get a quotient j and a remainder k. 
    int j = c/4; 
    int k = c % 4; 

    // Divide a + 11 * h by 319 to get a quotient m. Ignore the remainder. 
    int m = (a + 11 * h)/319; 

    // Divide 2 * e + 2 * j - k - h + m + 32 by 7 to get a remainder r. Ignore the quotient. 
    int r = (2 * e + 2 * j - k - h + m + 32) % 7; 

    // Divide h - m + r + 90 by 25 to get a quotient n. Ignore the remainder. 
    n = (h - m + r + 90)/25; 

    // Divide h - m + r + n + 19 by 32 to get a remainder p. 
    p = (h - m + r + n + 19) % 32; 

} 
/** 
* 
* @return n returns the month in which a given year's Easter Sunday will take place 
*/ 
public int getEasterSundayMonth() { 

    return n; 
} 
/** 
* 
* @return p returns the day in which a given year's Easter Sunday will take place 
*/ 
public int getEasterSundayDay() { 

    return p; 
} 

}

这里是Tester类:

公共类EasterTester {

public static void main(String[] args) 

    { 
     Easter myEaster = new Easter(2002); 

     System.out.println("In 2002, Easter Sunday is: " + "month = " + myEaster.getEasterSundayMonth() + " and day = " + myEaster.getEasterSundayDay()); 

     Easter myEaster2 = new Easter(2012); 

     System.out.println("In 2012, Easter Sunday is: " + "month = " + myEaster2.getEasterSundayMonth() + " and day = " + myEaster2.getEasterSundayDay()); 

    } 
} 
+0

测试仪类不_test_什么。 –

+0

@TrueSoft我认为它测试了2002年和2012年?这个意见是什么意思? – dustdustdust

+0

测试人员应该测试结果是否与预期结果相同。了解如何制作[JUnit测试](http://www.vogella.com/articles/JUnit/article.html)(另请参阅[测试驱动开发](http://www.agiledata.org/)文章/ tdd.html))。否则,你可以像这样测试:'int expectedMonth_2012 = 4; if(myEaster2.getEasterSundayMonth()== expectedMonth_2012)System.out.println(“Passed”);否则System.err.println(“失败;预计月份+ expectedMonth_2012 +”,但返回“+ myEaster2.getEasterSundayMonth());' –

回答

4

本书的使用UML这样的:

calculateEaster(int aYear): String 

真的只是意味着你有一个这样的公共方法:

public String calculateEaster(int aYear) 

(参数名是可怕的, 顺便一提。如果这本书使用的amy名之前的前缀,an,或the建议,请忽略它...可能获得更好的书。)

我要说的是,界面会好得多(以Java的语法)

public LocalDate calculateEaster(int year) 

...使用Joda TimeLocalDate类。如果你不想使用,使其返回java.util.Calendar或潜在的一个java.util.Date。 (这些类别都不是真正意义上的名称,也不是理想的,但我们可以去......)

...而不是像本书推荐的那样返回String。然而,这是从根本上的哪些对象的实例而言意味着一个区别。在你的情况下,它代表了一年中的某一天(尽管它不记得其中年,这是奇数)。本书的建议是,不应该有任何实例状态 - 你会构建一个EasterCalculator而不是代表一个复活节的单个实例。

顺便说一句,这个代码是坏:

/** 
* @param n is the month 
* @param p is the day 
*/ 
private int n; 
private int p; 

你的Javadoc注释正试图就好像它是一个方法有两个参数记录单。对于有效的Javadoc,你会想:

/** The month of the year */ 
private int n; 

/** The day of the month */ 
private int p; 

然而,事实场需要文档化表示,他们正在严重命名。你为什么不给他们拨打monthday呢?

+0

谢谢你的答案。我现在明白javadoc的使用更好;非常感谢你!此外,我注意到我的代码在该网站上过期,因此我将从此处将其托管在此处。我同意这些字段的命名很糟糕;我只使用了我的练习中提出的名字,对不起!另一个问题,方法: public String calculateEaster(int aYarar) 如何将其输入到程序中?我一般熟悉实现方法和类,但本身并不是字符串。为了使我的编辑代码正常工作,我仍然很困惑从哪里出发。 – dustdustdust

+0

@dustdustdust:您应该将其作为一个单独的问题,并提供*更多详细信息。目前还不清楚你的意思,当你真的想问一个不同的问题时,在这里详细讨论并不合适。 –

+0

“我不明白这个评论,因此,我不明白如何编辑我的代码才能遵循这些指导原则。”我相信这是同一个问题。比代码的长度更多的细节你暗示的缺席? – dustdustdust