// 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);
}
}
最佳答案
我可以理解为什么您提供的模板对于初学者来说可能会有问题:一方面,months-array 不是静态的,另一方面,您的重载构造函数在必须委托(delegate)给 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()。
关于Java:我需要有关 Date 类的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5230686/