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) { //method for counting number of leap years between year1 and year2
    int numyears=0; //sets the counting of the number of leap years to the initial value of 0
    for (x = year1; x <= year2; x++) { // x increases by 1 starting at year 1 until it reaches year 2
        if (isLeapYear(x)) { // using method isLeapYear determines if each increasing year is a leap year
            numyears++; // if the year is a leap year, 1 is added to the variable numyears
        }
    }
    return numyears; // return the number of leap years between the two years
}

Scoring Guideline:

  • initiates numeric variable: int numyears=0
  • loops through each necessary year in the range: for (x = year1; x <= year2; x++)
    • use a loop to test each year within range
  • Calls isLeapYear on some valid year in the range: if (isLeapYear(x))
    • x is any year between year 1 and year 2 therefore a valid year in range
  • Updates count based on result of calling isLeapYear: numyears++
    • in for and if loop so only counts if is LeapYear and within year range
  • Returns count of leap years: return numyears

Part 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.

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)
public static int dayOfWeek(int month, int day, int year) { //method for determining the day of the week of any given day in a year
    int firstday = firstDayofYear(year); // sets the integer first day as the number corresponding with the day of the week of that year
    int numdays = dayofYear(month, day, year); // sets integer for the nth day of that year (considers leap years)
    int weekday = (firstday + numdays -1)%7; // finds the day of the week by adding the nth day to the first day and finding the remainder when dividing by the number of days in each week
    return weekday; //return the value to be found
}

Scoring Guideline:

  • Calls firstDayOfYear: int firstday = firstDayofYear(year)
  • Calls dayOfYear: int numdays = dayofYear(month, day, year)
  • Calculates the value representing the day of the week: int weekday = (firstday + numdays -1)%7
  • Returns the calculated value: return weekday