You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB

// The IncDate class
// Do not make changes to anything other than the body of increment() method
// Your name here
public class IncDate extends Date
{
// copy constructor
public IncDate(Date o)
{
super(o.m_month, o.m_day, o.m_year);
}
// constructor
public IncDate(int month, int day, int year)
{
super(month, day, year);
}
public void addDays(int numDays)
{
// TODO: implement this method
int days_to_add = numDays;
int[] MONTHS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0));
for (int current_month = 0; current_month < m_month-1; current_month ++)
days_to_add += MONTHS[current_month];
if ((m_month > 2) && is_leap)
days_to_add += 1;
days_to_add += m_day-1;
m_month = 1;
m_day = 1;
while (days_to_add > 365) {
is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0));
if (is_leap)
days_to_add -= 1;
days_to_add -= 365;
m_year += 1;
}
is_leap = (m_year % 400 == 0) || ((m_year % 4 == 0) && !(m_year % 100 == 0));
if (is_leap)
MONTHS[1] +=1;
while (days_to_add >= MONTHS[m_month-1]) {
days_to_add -= MONTHS[m_month-1];
m_month += 1;
}
m_day=days_to_add+1;
}
}