FRQ 1 - 2019 Exam
- The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class. #### (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.
public static int numberOfLeapYears(int year1, int year2){
int numOfLeapYears = 0; // initialize final count variable
for(int i = year1; i <= year2; i++){ // for loop that starts at the year1 number and goes until year 2 including it
if(isLeapYear(i)){ // if the year is a leap year, add 1 to the count
numOfLeapYears += 1;
}
}
return numOfLeapYears; // return the count
}
(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.
public static int dayOfWeek(int month, int day, int year){
int date = dayOfYear(month, day, year); // gets current day number
int first = firstDayofYear(year); // gets starting day of year
int calculated = ((week + start)-1) % 7; // adds starting and current and subtracts one to account for the offset, and divides by 7. remainder determines the day of week
return calculated;
}