// 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);
}
}
1
A
回答
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
好吧,關於(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日。
相關問題
- 1. 需要Java幫助
- 2. 需要Java幫助
- 3. 需要幫助JAVA
- 4. 我需要我的Java程序幫助
- 5. 在jquery date picker中需要幫助
- 6. 我需要Android和Java的幫助
- 7. 我需要在java中的幫助
- 8. 我需要此Java Array的幫助
- 9. 我需要使用Java的FTP幫助
- 10. 需要幫助的Java
- 11. 的Java Swing需要幫助
- 12. 需要幫助的在Java
- 13. 需要幫助測試我的類
- 14. 需要ManifestResourceInfo類的幫助
- 15. 我需要幫助理解java代碼
- 16. 我需要幫助的codechef
- 17. Java - 需要幫助java.lang.ArrayIndexOutOfBoundsException
- 18. 需要幫助Java中
- 19. 需要String Java幫助
- 20. 需要Java OCR幫助
- 21. 需要幫助在Java
- 22. 需要幫助Java作業
- 23. 幫助需要在Java
- 24. 在Java中需要幫助
- 25. Java CMS需要幫助
- 26. GCM + Android + Java幫助需要
- 27. 需要java編程幫助!
- 28. 需要幫助在Java
- 29. JAVA/JAXB需要幫助
- 30. 需要幫助Java中
你真的不會試一試嗎? – RAY 2011-03-08 09:42:16
我正在嘗試,但我是堆棧。 – Mugetsu 2011-03-08 09:43:40
@Nishant我有一些答案是平行的,所以我沒有選擇答案。 – Mugetsu 2011-03-08 09:44:33