// 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