我写了一个名为Easter的类(见下文)和一个名为EasterTester的测试器类(也在下面)来执行它并插入年份值。目标是实施高斯算法,用于计算任何指定年份的复活节星期日和月份。
我的代码工作正常,但我对我正在关注的书有些困惑。它告诉我不要在我的第一个代码链接的底部实现两个getter方法,因为我“不需要它们”。正如我的代码所示,我绝对需要它们。有什么我想念的吗?
此外,它列出了我建议用作的“复活节类公共接口”:
calculateEaster(int aYear):String
“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 CLASS:
公共课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 :(得分:4)
本书对UML的使用如下:
calculateEaster(int aYear): String
真的只是意味着你有一个像这样的公共方法:
public String calculateEaster(int aYear)
(顺便说一句,参数名称很糟糕。如果图书建议在名称前使用a
前缀,an
,my
或the
,请忽略它...并且可能会得到一本更好的书。)
我认为接口会更好(在Java语法中)
public LocalDate calculateEaster(int year)
...使用Joda Time的LocalDate
课程。如果您不想使用它,请返回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;
但是,字段需要记录的事实表明它们的名称很糟糕。你为什么不打电话给他们month
和day
?