Java和UML方法语法中的复活节日期

Java和UML方法语法中的复活节日期,java,class,date,methods,uml,Java,Class,Date,Methods,Uml,我编写了一个名为Easter(见下文)的类和一个名为EasterTester(也在下文)的测试程序类来执行它并插入年份值。目标是实现Gauss算法,计算任何特定年份复活节周日的月份和日期 我的代码运行得很好,但我对我正在阅读的书有点困惑。它告诉我不要在第一个代码链接的底部实现两个getter方法,因为我“不需要它们”。在这方面我有什么遗漏吗 此外,它还列出了建议我使用的“复活节类公共接口”: CalculateMaster(整年):字符串 “UML方法语法表示该方法接受一个整数参数并返回一个字符

我编写了一个名为Easter(见下文)的类和一个名为EasterTester(也在下文)的测试程序类来执行它并插入年份值。目标是实现Gauss算法,计算任何特定年份复活节周日的月份和日期

我的代码运行得很好,但我对我正在阅读的书有点困惑。它告诉我不要在第一个代码链接的底部实现两个getter方法,因为我“不需要它们”。在这方面我有什么遗漏吗

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

CalculateMaster(整年):字符串


“UML方法语法表示该方法接受一个整数参数并返回一个字符串。”我不理解这个注释,因此,我不理解如何编辑代码以遵循这些准则

如果有人能澄清这个难题/问题,我将不胜感激

代码: /** * *@author b\u 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类:

伊斯特斯特公务舱{

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());

   }
}

本书对UML的使用如下:

calculateEaster(int aYear): String
public String calculateEaster(int aYear)
实际上,这意味着您将有一个如下的公共方法:

calculateEaster(int aYear): String
public String calculateEaster(int aYear)
(顺便说一句,参数名很糟糕。如果这本书建议在名称前使用前缀
a
a
my
The
,请忽略它……并可能得到一本更好的书。)

我认为接口会比(Java语法中的)好得多

…使用的
LocalDate
类。如果您不想使用它,请让它返回
java.util.Calendar
或可能返回
java.util.Date
(这两个类都不是真正意义上的名称,也都不是理想的,但我们开始了…)

…而不是像书中推荐的那样返回
字符串
。但是,从根本上讲,它与对象实例的含义不同。在您的例子中,它表示一年中的一天(尽管它不记得是哪一年,这很奇怪).这本书的建议是不应该有任何实例状态-您应该构建一个
EasterCalculator
,而不是表示一个Easter实例

顺便说一句,这段代码很糟糕:

/**
 * @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;

然而,字段需要文档化的事实表明它们的名称不正确。为什么不直接称它们为
month
day

calculateEaster(int aYear): String
public String calculateEaster(int aYear)
实际上,这意味着您将有一个如下的公共方法:

calculateEaster(int aYear): String
public String calculateEaster(int aYear)
(顺便说一句,参数名很糟糕。如果这本书建议在名称前使用前缀
a
a
my
The
,请忽略它……并可能得到一本更好的书。)

我认为接口会比(Java语法中的)好得多

…使用的
LocalDate
类。如果您不想使用它,请让它返回
java.util.Calendar
或可能返回
java.util.Date
(这两个类都不是真正意义上的名称,也都不是理想的,但我们开始了…)

…而不是像书中推荐的那样返回
字符串
。但是,从根本上讲,它与对象实例的含义不同。在您的例子中,它表示一年中的一天(尽管它不记得是哪一年,这很奇怪).这本书的建议是不应该有任何实例状态-您应该构建一个
EasterCalculator
,而不是表示一个Easter实例

顺便说一句,这段代码很糟糕:

/**
 * @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;

但是,字段需要记录的事实表明它们的名称不正确。为什么不直接称它们为
month
day

tester类不测试任何东西。@TrueSoft我认为它测试的是2002年和2012年?你说这句话是什么意思?tester类应该测试结果是否为e与预期的相同。看看如何进行测试(也可以阅读)。否则您可以这样测试:
int expectedMonth\u 2012=4;if(myaster2.getEasterSundayMonth()==expectedMonth\u 2012)System.out.println(“已通过”);else System.err.println(“失败;预期月份”+expectedMonth\u 2012+”,但返回”+myEaster2.getEasterSundayMonth());
tester类不测试任何东西。@TrueSoft我以为它测试的是2002年和2012年的数据?你这句话是什么意思?一个tester类应该测试结果是否与预期的结果相等。请看如何进行测试(另外,请阅读)。否则您可以这样测试:
int expectedMonth\u 2012=4;if(myaster2.getEasterSundayMonth()==expectedMonth\u 2012)System.out.println(“通过”);else System.err.println(“失败;预期月份”+expectedMonth\u 2012+”,但返回“+myaster2.getEasterSundayMonth())
谢谢你的回答。我现在更好地理解了javadoc的用法;非常感谢!我注意到我的代码在该网站上过期了,所以我将从这里开始托管它。我同意字段名称不正确;我只是使用了我的练习建议的名称,对不起!另一个问题,方法:public String calculateEaster(int aYear)如何将其输入程序?我通常熟悉实现方法和类,但不熟悉字符串本身。仍然很困惑如何才能使我编辑的代码正常工作。@DustDust:你应该作为一个单独的问题来问,并提供更多的细节。你不清楚你在问什么