APCalendar is #1 from the from the 2019 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/pdf/ap19-frq-computer-science-a.pdf?course=ap-computer-science-a

Part (a) numberOfLeapYears method

public static int numberOfLeapYears(int year1, int year2)
{
    int leapYears = 0;

    for(int y = year1; y <= year2; y++)
        if(isLeapYear(y))
            leapYears++;

    return leapYears;
}

Part (b) dayOfWeek method (original solution)

public static int dayOfWeek(int month, int day, int year)
{
    int weekday = firstDayOfYear(year);
    int additionalDays = dayOfYear(month, day, year) - 1;

    for(int d = 1; d <= additionalDays; d++)
    {
        weekday++;

        if(weekday == 7)
            weekday = 0;
    }

    return weekday;
}

Part (b) dayOfWeek method (considered solution)

public static int dayOfWeek(int month, int day, int year)
{
    int additionalDays = dayOfYear(month, day, year) - 1;
    return (firstDayOfYear(year) + additionalDays) % 7;
}

I didn’t see this solution when I first approached the problem. When I looked at my original solution a half hour later, I realized that it could obviously be simplified to this.

I’ve received a number of questions about whether alternate solutions to dayOfWeek work. Please find my JUnit3 test below. It will run in Eclipse or any other IDE that supports JUnit3. Replace my code for the method with yours.

APCalendar.java
APCalendarTest.java

2019 AP CS Exam Free Response Solutions

Comments

Comment on APCalendar