// 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);
}
}
答案 0 :(得分:1)
我可以理解为什么给出的模板对初学者来说可能有问题:一方面,months-array不是静态的,另一方面你的重载构造函数在它们之前无法访问它必须委托给this(int,int,int)-constructor。
要修复必须声明month-array static的代码,请创建一个将month-Strings转换为month-number的静态方法。此外,您必须注释掉特定行以编译和测试此。
让我们一起解决(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()。
答案 1 :(得分:0)
好的,关于(iii):
我假设您不允许使用java Date类,因此您必须这样做:要将DDD转换为月份和日期,您必须首先在该特定日期中定义一个月 - 日数组一年,这当然取决于当年2月是28天还是29天。在正常情况下就是这样:
int [] monthDays = new int [12] {31,28,31,30,31,30,31,31,30,31,30,31}
如果年份被4整除,除了100之外,除了400(参见维基百科的闰年),你必须将28改为29。
好的,要转换你的DDD你可以这样做:通过那个数组,直到你的DDD小于当前单元格中存储的值减去当前单元格并记住你向前移动了一个月。示例:DDD = 41 - &gt;减去31 - &gt;这个日期是2月10日。