2011-03-08 117 views
1
// Date3.java 
// Date3 class declaration. 

public class Date3 
{ 
    private int month; // 1-12 
    private int day; // 1-31 based on month 
    private int year; // any year 
    private String[] months = new String[]{ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; 


    // constructor: call checkMonth to confirm proper value for month; 
    // call checkDay to confirm proper value for day 
    public Date3(int theMonth, int theDay, int theYear) 
    { 
     month = checkMonth(theMonth); // validate month 
     year = theYear; // could validate year 
     day = checkDay(theDay); // validate day 

     System.out.printf( 
     "Date3 object constructor for date %s\n", this); 
    } // end Date3 constructor 

    public Date3(String m, int d, int y){ 
     this(m, d, y); 
    } 

    public Date3(int m, int y){ 
     this(m,0, y); 
    } 


    // utility method to confirm proper month value 
    private int checkMonth(int testMonth) 
    { 
     if (testMonth > 0 && testMonth <= 12) // validate month 
     return testMonth; 
     else // month is invalid 
     { 
     System.out.printf( 
      "Invalid month (%d) set to 1.", testMonth); 
     return 1; // maintain object in consistent state 
     } // end else 
    } // end method checkMonth 

    // utility method to confirm proper day value based on month and year 
    private int checkDay(int testDay) 
    { 
     int[] daysPerMonth = 
     { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 

     // check if day in range for month 
     if (testDay > 0 && testDay <= daysPerMonth[ month ]) 
     return testDay; 

     // check for leap year 
     if (month == 2 && testDay == 29 && (year % 400 == 0 || 
      (year % 4 == 0 && year % 100 != 0))) 
     return testDay; 

     System.out.printf("Invalid day (%d) set to 1.", testDay); 
     return 1; // maintain object in consistent state 
    } // end method checkDay 

    public String getMonthString(int month){ 
     return months[month]; 
     } 


    /* public String monthAsString() 
    { 
     //returns month as a string rather than an integer 
     switch (month) 
     { 
      case 1: return "January"; 
      case 2: return "February"; 
      case 3: return "March"; 
      case 4: return "April"; 
      case 5: return "May"; 
      case 6: return "June"; 
      case 7: return "July"; 
      case 8: return "August"; 
      case 9: return "September"; 
      case 10: return "October"; 
      case 11: return "November"; 
      case 12: return "December"; 
      default: return "";   
     }   
    }*/ 




    // return a String of the form month/day/year 
    public String toString() 
    { 
     return String.format("%d/%d/%d", month, day, year); 
    } // end method toString 
} // end class Date3 





public class Date3Test { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Date3 myDate = new Date3(9, 16, 2011); 
     Date3 myDate2 = new Date3("June", 24, 2010); 
     Date3 myDate3 = new Date3(259, 2005); 

     // all three dates above are equal and will therefore print 
     // the same dates 

     System.out.println(myDate); 
     System.out.println(myDate2); 
     System.out.println(myDate3); 

    } 

} 
+2

你真的不會試一試嗎? – RAY 2011-03-08 09:42:16

+0

我正在嘗試,但我是堆棧。 – Mugetsu 2011-03-08 09:43:40

+0

@Nishant我有一些答案是平行的,所以我沒有選擇答案。 – Mugetsu 2011-03-08 09:44:33

回答

1

我能理解爲什麼你提供的模板可能對於初學者是有問題的:一方面,幾個月陣也不是一成不變的,在另一方面你超載構造函數在它們必須委託給this(int,int,int) - 構造函數之前無法訪問它。

要修復代碼,您必須聲明月份數組靜態,請創建一個將您的月份字符串轉換爲月份數的靜態方法。此外,你必須註釋掉特定的行甚至編譯和測試這個

因此,讓我們解決(II)一起

替換:

private String[] months 

有:

private static String[] months 

替換:

public Date3(String m, int d, int y){ 
    this(m, d, y); 
} 

有:

public Date3(String m, int d, int y){ 
    this(convMonth(m), d, y); 
} 
public static int convMonth(String m) { 
    int index =1; // january will be 1 
    for(String month : months) { 
     if(m.equals(month)) break; 
     index++; 
    } 
    return index; 
} 

註釋掉第二個構造模板,像這樣:

/*public Date3(int m, int y){ 
    this(m,0, y); 
}*/ 

而且註釋掉在Date3Test.java調用到第二個構造模板:

//System.out.println(myDate3); 

現在這應該編譯並運行,併爲你提供(ii)的幫助。你可以自己解決(iii),只需實現註釋掉的第二個構造函數模板並重新激活println()。

+0

謝謝。你能給我最後一個小費嗎? – Mugetsu 2011-03-09 03:10:36

+1

想幫忙,但我看不到你的(iii)了。 – 2011-03-09 14:17:19

+0

這裏是:iii)DDD YYYY – Mugetsu 2011-03-14 01:44:06

0

好吧,關於(III):

我認爲你不能使用java Date類,所以你必須這樣做:以DDD轉換爲月份和日期的日,你將有首先定義該年份的月份日期陣列,這當然取決於當年的2月份是28天還是29天。在正常情況下它是這樣的:

INT [] monthDays =新INT [12] {31,28,31,30,31,30,31,31,30,31,30,31}

年被4整除的情況下,除了那些100,除由400(參見維基百科爲閏年),你將不得不改變28至29

好吧,你DDD你轉換可以這樣做:遍歷該數組,直到您的DDD小於當前單元格中存儲的值,然後減去當前單元格,並記住您向前移動了一個月。例如:DDD = 41 - >減去31 - >日期是2月10日。