FRQ 1
2019 FRQ Question 1
Part 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)
public static int numberOfLeapYears(int year1, int year2) {
for(int i = year1; i <= year2; i++){
int countLeapYears=0;
if(isLeapyear(i)){
countLeapYears+=1;
}
return countLeapYears;
}
}
Part b
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)
public static int dayOfWeek(int month, int day, int year){
int startDay = firstDayOfYear(year);
int nthDay = dayOfYear(month, day, year);
int returnDay = (startDay + nthDay - 1) % 7;//start day + number of days plus leftover days are used to calculate final day
return returnDay;
}