2012-09-24 26 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