// Prototype Implementation
public class APCalendar {
/** Returns true if year is a leap year and false otherwise.
* isLeapYear(2019) returns False
* isLeapYear(2016) returns True
*/
private static boolean isLeapYear(int year) {
// implementation not shown
boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
if(isLeapYear){
return true;
}
else{
return false;
}
}
/** Returns the value representing the day of the week
* 0 denotes Sunday,
* 1 denotes Monday, ...,
* 6 denotes Saturday.
* firstDayOfYear(2019) returns 2 for Tuesday.
*/
private static int firstDayOfYear(int year) {
// implementation not shown
return 0;
}
/** Returns n, where month, day, and year specify the nth day of the year.
* This method accounts for whether year is a leap year.
* dayOfYear(1, 1, 2019) return 1
* dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year
* dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year.
*/
private static int dayOfYear(int month, int day, int year) {
// implementation not shown
int[] a = {31, 29, 31, 30, 31, 30, 31, 30,31 30, 30, 31 };
int[] b = {31, 28, 31, 30, 31, 30, 31, 30,31 30, 30, 31 };
if(isLeapYear(year)=true;){
for (int i = 0; i < month-1; i++) {
// Array.getInt method
int x = day;
x+=Array.getInt(a, i);
}
System.out.println("This is not a leap year");
}
else{
for (int i = 0; i < month-1; i++) {
// Array.getInt method
int x = day;
x+=Array.getInt(b, i);
}
System.out.println("This is a leap year");
}
System.out.println(x);
return 1;
}
/** 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)
int num=0;
for (int i=year1; i<=year2; i++)
{
if(isLeapYear(i)){
num++;
}
}
return num;
}
/** Returns the value representing the day of the week for the given date
* 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)
return 0;
}
/** Tester method */
public static void main(String[] args) {
// Private access modifiers
System.out.println("isLeapYear: " + APCalendar.isLeapYear(2020));
System.out.println("firstDayOfYear: " + APCalendar.firstDayOfYear(2022));
System.out.println("dayOfYear: " + APCalendar.dayOfYear(1, 1, 2022));
// Public access modifiers
System.out.println("numberOfLeapYears: " + APCalendar.numberOfLeapYears(2019, 2021));
System.out.println("dayOfWeek: " + APCalendar.dayOfWeek(1, 1, 2022));
}
}
APCalendar.main(null)