APCalendar free response 2

APCalendar free response problem from the 2019 AP Computer Science A Exam.

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 this method 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 Answers

2 thoughts on “APCalendar free response

  1. Alex May 19,2019 4:34 pm

    for part b. i did this:


    int fday = firstdayofyear(year);
    int modr = dayofyear(month, day, year) %7;
    if( (fday+modr)-1>7)
      return ((fday +modr)-1 )-7;
    else
      return (fday +modr)-1;

    first, would this be acceptable
    second, im pretty sure i forgot to put >= 7, so how many points would i lose for putting just >7?

    • Brandon Horn May 20,2019 9:58 am

      Hi Alex,

      As your proposed solution appears (with > 7) it fails for dates such as:

      1/6/2019
      correct: 0
      yours: 7

      With >= 7, your proposed solution fails for dates such as:

      1/7/2006
      correct: 6
      yours: -1

      As of this writing the College Board has not yet released scoring guidelines for 2019.

Comments are closed.