I'm suppose to combine class Time2 and class date into one class DateAndTime. I don't understand how to do it at all, I've been trying to read through my book and I just don't get it. Can anyone possibly help me out on how i could combine these two classes? The time2 class increments the time, and the date class increments the days, how would i combine this so that if the time reaches the next day it increments into the next day? I'm so lost as to how to go about this.
public class Date extends Time2
{
private int month; // 1-12
private int day; // 1-31 based on the month
private int year; // any year
// constructor: call checkMonth to confirm proper value for month;
// call checkDay to confirm proper value for the day
public Date(int theMonth, int theDay, int theYear)
{
month = checkMonth(theMonth); // validate month
year = checkYear(theYear); //could validate the year
day = checkDay(theDay); //validate day
} // end Date constructor
// 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
{
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;
return 1; //maintain object in consistent state
} //end method checkDay
private int checkYear (int testYear)
{
return testYear !=0 ? testYear : 1;
}//end checkYear
public int getDay()
{
return day;
}//end getDay
public int getMonth()
{
return month;
}// end getMonth
public int getYear()
{
return year;
}// end getYear
public void setDay(int d)
{
day = checkDay(d);
}//end setDay
public void setMonth(int m)
{
month = checkMonth(m);
}//end setMonth
public void setYear(int y)
{
year = checkYear(y);
}// end setYear
public void nextDay()
{
day = checkDay(day + 1);
if (day == 1) nextMonth();
}// end nextDay
public void nextMonth()
{
month = checkMonth(month + 1);
if (month == 1) nextYear();
}// end nextMonth
public void nextYear()
{
year = checkYear(year + 1);
}
//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
public class Time2
{
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
// Time2 no-argument constructor: initializes each instance variable
// to zero; ensures that Time2 objects start in a consistent state
public Time2()
{
this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 no-argument constructor
// Time2 constructor: hour supplied, minute and second defaulted to 0
public Time2( int h )
{
this( h, 0, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 one-argument constructor
// Time2 constructor: hour and minute supplied, second defaulted to 0
public Time2( int h, int m )
{
this( h, m, 0 ); // invoke Time2 constructor with three arguments
} // end Time2 two-argument constructor
// Time2 constructor: hour, minute and second supplied
public Time2( int h, int m, int s )
{
setTime( h, m, s ); // invoke setTime to validate time
} // end Time2 three-argument constructor
// Time2 constructor: another Time2 object supplied
public Time2( Time2 time )
{
// invoke Time2 three-argument constructor
this( time.getHour(), time.getMinute(), time.getSecond() );
} // end Time2 constructor with a Time2 object argument
// Set Methods
// set a new time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
public void setTime( int h, int m, int s )
{
setHour( h ); // set the hour
setMinute( m ); // set the minute
setSecond( s ); // set the second
} // end method setTime
// validate and set hour
public void setHour( int h )
{
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
} // end method setHour
// validate and set minute
public void setMinute( int m )
{
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
} // end method setMinute
// validate and set second
public void setSecond( int s )
{
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
} // end method setSecond
// Get Methods
// get hour value
public int getHour()
{
return hour;
} // end method getHour
// get minute value
public int getMinute()
{
return minute;
} // end method getMinute
// get second value
public int getSecond()
{
return second;
} // end method getSecond
public void tick()
{
setSecond(second + 1);
if (second == 0) incrementMinute();
}
public void incrementMinute()
{
setMinute(minute + 1);
if (minute == 0) incrementHour();
}
public void incrementHour()
{
setHour(hour + 1);
if (hour ==0)
nextDay();
}
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
} // end method toUniversalString