1. 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
}

Scoring (5/5)

  • Initialize numeric variable (count) 1/1
  • Loop through each necessary year in range (for loop, with condition in code block) 1/1
  • Calls isLeapYear on valid year in range 1/1
  • Update count based on result of isLeapYear 1/1
  • Return count of leap years 1/1

(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;
}

Scoring (4/4)

  • Call firstDayofYear 1/1
  • Calls dayOfYear 1/1
  • Calculate value representing day of week (-1, incorrect calculation) 1/1
  • Return calculated value 1/1