我编写了一个名为 Easter 的类(见下文)和一个名为 EasterTester 的测试器类(也在下面)来执行它并插入年份值。目标是实现高斯算法,计算任何指定年份的复活节星期天的月份和日期。
我的代码工作正常,但我对我正在关注的书有点困惑。它告诉我不要实现第一个代码链接底部的两个 getter 方法,因为我“不需要它们”。就我的代码而言,我绝对需要它们。有什么我错过的吗?
此外,它列出了我建议用作的“复活节类公共接口”:
计算复活节(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;
}
}
这里是测试类:
公共类 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());
}
}