Java: I need help on Date class
// 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);
}
}
I can understand why the template you were given might be problematic for a beginner: On the one hand the months-array is not static, on the other hand your overloaded constructors cannot access it before they have to delegate to the this(int,int,int)-constructor.
To fix the code you have to declare the months-array static, create a static method that converts your month-Strings to month-number. Further, you have to comment-out specific lines to even compile and test this.
So let's solve (ii) together
Replace:
private String[] months
with:
private static String[] months
Replace:
public Date3( String m, int d, int y){
this(m, d, y);
}
with:
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;
}
Comment-out the second constructor-template like so:
/*public Date3( int m, int y){
this(m,0, y);
}*/
And comment-out the call to that second constructor-template in Date3Test.java:
//System.out.println(myDate3);
Now this should compile and run and give you the anwer for (ii). You can solve (iii) yourself, just implement the commented-out second constructor-template and re-activate that println().
Ok, regarding (iii):
I assume you are not allowed to use the java Date class, so you would have to do this: to convert DDD into month and day-of-month you will have to first define an array of month-days in that particular year, this depends of course whether the february of that year has 28 or 29 days. In the normal case it's this:
int[] monthDays = new int[12] {31,28,31,30,31,30,31,31,30,31,30,31}
In the case of years divisible by 4, except those by 100, except by 400 (see wikipedia for leap year), you will have to change that 28 to 29.
Ok, to convert your DDD you can do this: Go through that array and until your DDD is smaller than the value stored in the current cell subtract the current cell and remember that you moved one month forward. Example: DDD=41 -> subtract 31 -> the date is february 10th.
精彩评论