2019 FRQ Question 1

This FRQ type is labeled as a Method and Control Structure which covers (as implied) methods (functions) and control structures (if else, loops, etc.). This question deals with the creation of a calendar in code. It's interesting, at least to me, because it deals with a pretty standard yet extremely useful everyday tool, a calendar and after completing it, I found calendars to be surprisingly straight forward in terms of backend calculations (more on that in my reflection).

Instructions are labeled below while an explanation of my approach and code is down below. Peer grades are below after both parts to the question. At the very end is a reflection for the FRQ. See the TOC to jump to each section.

Question 1

Instructions (It hurts my eyes and brain to read :')

  1. The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class. public class APCalendar { / Returns true if year is a leap year and false otherwise. / private static boolean isLeapYear(int year) { / implementation not shown */ } / Returns the number of leap years between year1 and year2, inclusive.
  • Precondition:0 <= year1 <= year2> / public static int numberOfLeapYears(int year1, int year2) { / to be implemented in part (a) */ } /** Returns the value representing the day of the week for the first day of year,
  • where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. / private static int firstDayOfYear(int year) { / implementation not shown */ } /** Returns n, where month, day, and year specify the nth day of the year.
  • Returns 1 for January 1 (month = 1, day = 1) of any year.
  • Precondition:The date represented by month, day, year is a valid date.> / private static int dayOfYear(int month, int day, int year) { / implementation not shown */ } /** Returns the value representing the day of the week for the given date
  • (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
  • and 6 denotes Saturday.
  • Precondition:The date represented by month, day, year is a valid date.> / public static int dayOfWeek(int month, int day, int year) { / to be implemented in part (b) */ } // There may be instance variables, constructors, and other methods not shown. }

Part A

(a) Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive. In order to calculate this value, a helper method is provided for you. • isLeapYear(year) returns true if year is a leap year and false otherwise. Complete method numberOfLeapYears below. You must use isLeapYear appropriately to receive full credit. /** Returns the number of leap years between year1 and year2, inclusive.

  • Precondition:0 <= year1 <= year2> */ public static int numberOfLeapYears(int year1, int year2)

Approach to Part A

Note to Self: Preconditions means that the following condition is assumed to be met when inputs are implemented. Therefore, it is redundant and unnecessary to specify an if statement to check the Preconditions

They provide the function isLeapYear which significantly eases how much I need to do b/c finding if it is a leap year or not would hurt my brain to code and check. Therefore, the only concern would be the function which they ask for which outputs the number of leap years on a range of years from year1 to year2.

First we create our method signature (a static method as asked) and set its return type as an integer (since it'll return the # of years that are actually leap years) w/ the arguments year 1 and year 2 (the range of years). Since we are returning the # of years that are leap years, we can instantiate a int variable, leapYearCounter, to 0.

Now that we're done with creating the base of the method, we can start w/ the meat of the method. Since we can check if each year is a leap year with isLeapYear(), we simply need to iterate through each year in the range (inclusive) and pass them through isLeapYear(). If the isLeapYear() returns true, we add 1 to our leapYearCounter. The result should be a the number of leap years being recorded in leapYearCounter and thus we can return that value as the output.

public static int numberOfLeapYears(int year1, int year2){
    int leapYearCounter = 0;
        for(int year = year1; year <= year2; year++){
            if(isLeapYear(year)){
                leapYearCounter++;
            }
        }

    return leapYearCounter;
}

Part B

(b) Write the static method dayOfWeek, which returns the integer value representing the day of the week for the given date (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.

For example, 2019 began on a Tuesday, and January 5 is the fifth day of 2019. As a result, January 5, 2019, fell on a Saturday, and the method call dayOfWeek(1, 5, 2019) returns 6.

As another example, January 10 is the tenth day of 2019. As a result, January 10, 2019, fell on a Thursday, and the method call dayOfWeek(1, 10, 2019) returns 4. In order to calculate this value, two helper methods are provided for you.

• firstDayOfYear(year) returns the integer value representing the day of the week for the first day of year, where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. For example, since 2019 began on a Tuesday, firstDayOfYear(2019) returns 2.

• dayOfYear(month, day, year) returns n, where month, day, and year specify the nth day of the year. For the first day of the year, January 1 (month = 1, day = 1), the value 1 is returned. This method accounts for whether year is a leap year. For example, dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year, while dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year.

Class information for this question

public class APCalendar private static boolean isLeapYear(int year) public static int numberOfLeapYears(int year1, int year2) private static int firstDayOfYear(int year) private static int dayOfYear(int month, int day, int year) public static int dayOfWeek(int month, int day, int year)

Complete method dayOfWeek below. You must use firstDayOfYear and dayOfYear appropriately to receive full credit.

/** Returns the value representing the day of the week for the given date

  • (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
  • and 6 denotes Saturday.
  • Precondition:The date represented by month, day, year is a valid date.> */ public static int dayOfWeek(int month, int day, int year)

Approach to Part B

Despite how ridiculously long Part B's instructions are, the solution that they ask for is simple with one line of code. The necessary information are the two functions they provide, firstDayOfYear(year) and dayOfYear(month, day, year), which finds the day of the week that a given year starts on in the form of an integer and the number of days that have passed since the beginning of a year given the month, day, and year, respectively.

Since we know that, we can instantiate a static method (as prompted for) that has a return type of int in order to represent the day of the week in integer form. We also pass in the arguments month, day, and year as necessary for the method dayOfYear.

Now, all that needs to be done is returning the day of the month. We can think of every complete week (7 days) of a given year as being negligible for this b/c it's a full week from Sunday to Saturday which we don't care about. Also, since we are notating days of the week in days from a range of 0-6, we can use modulo of 7 to determine the date of the year. In addition, the day of the week that the year starts on needs to be accounted. Therefore if we find the total number of days that have passed since the beginning of the year (dayOfYear(month, day, year) - 1) and add on the first day of the year, we'll have the total days from the last sunday before or on the first day of the year to the current day. Then, we can take modulo 7 to ignore each full 7 day week and produce an output which represents the day of the week.

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

Peer Grade

Reflection

Overall, this FRQ was not too difficult. It followed a pretty straight forward problem which I needed to solve the solution of. The easiest part was probably identifying which functions, variables, and return type I needed to use within the context of the problem. The hardest part was probably figuring out more minute logic within the control structures such as the bounds of the loop in the context of the problem or the proper expression to correctly produce the right day of the week.

Overall, practicing FRQs, as I likely will be throughout the year, on a completely new question format to me. The weirdest part will be writing out the code physically rather than having an IDE. I don't realize it totally but IDE's are so vital. They help me with so many random syntax errors I might make and create a clean way of formatting (smth I suck at ngl). They also give me insight into how College Board formats these types of questions which will be useful for the AP exam.

The best part I think I can take away from it, apart from being practice for an AP exam, is that it gives me the chance to apply problem solving skills and (arguable) creative skills in creating algorithms using Java syntax in the context of plausible problems.

For Methods and Control Structures in particular, the only thing I'd need to pay attention to and perhaps work on is the logic in the context of the problem which I'm likely prone to making mistakes in. Otherwise, I think I'm pretty well set up to complete the FRQs because of my familiarity with the concept of Methods and Control Structures.