The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class.

public class APCalendar {
    /** Returns true if year is a leap year and false otherwise. */
    private static boolean isLeapYear(int year) { /* implementation not shown */ }

    /** 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) */ }

    /** Returns the value representing the day of the week for the first day of year,
     * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.
     */
    private static int firstDayOfYear(int year) { /* implementation not shown */ }

    /** Returns n, where month, day, and year specify the nth day of the year.
     * Returns 1 for January 1 (month = 1, day = 1) of any year.
     * Precondition: The date represented by month, day, year is a valid date.
     */
    private static int dayOfYear(int month, int day, int year) { /* implementation not shown */ }

    /** 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) { /* to be implemented in part (b) */ }
}

(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) {
    // Your implementation here
}

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

Complete method dayOfWeek below. You must use firstDayOfYear and dayOfYear appropriately to receive full credit.

public static int dayOfWeek(int month, int day, int year) {
    // Your implementation here
}
import java.util.Scanner;

public class CalendarApp {
    // Returns the day of the week (0 for Sunday, 1 for Monday, ..., 6 for Saturday)
    private static int getDayOfWeek(int month, int day, int year) {
        int firstDayOfYear = firstDayOfYear(year);
        int daysIntoYear = dayOfYear(month, day, year);
        return (firstDayOfYear + daysIntoYear - 1) % 7;
    }

    // Returns the day of the week as a string
    private static String getDayOfWeekString(int dayOfWeek) {
        String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        return daysOfWeek[dayOfWeek];
    }

    // Returns the first day of the year (0 for Sunday, 1 for Monday, ..., 6 for Saturday)
    private static int firstDayOfYear(int year) {
        int day = (year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;
        return day < 0 ? day + 7 : day;
    }

    // Returns n, where month, day, and year specify the nth day of the year.
    private static int dayOfYear(int month, int day, int year) {
        int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
        if (isLeapYear(year)) {
            daysInMonth[2] = 29;
        }
        int daysIntoYear = day;
        for (int i = 1; i < month; i++) {
            daysIntoYear += daysInMonth[i];
        }
        return daysIntoYear;
    }

    // Checks if the given year is a leap year
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    // Returns the number of leap years between year1 and year2, inclusive.
    private static int numberOfLeapYears(int year1, int year2) {
        int leapYears = 0;
        for (int year = year1; year <= year2; year++) {
            if (isLeapYear(year)) {
                leapYears++;
            }
        }
        return leapYears;
    }

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("Welcome to CalendarApp!");
            System.out.println("Please enter a date (MM DD YYYY):");
            int month = scanner.nextInt();
            int day = scanner.nextInt();
            int year = scanner.nextInt();
    
            int dayOfWeek = getDayOfWeek(month, day, year);
            String dayOfWeekString = getDayOfWeekString(dayOfWeek);
            System.out.printf("The date %02d/%02d/%04d falls on a %s.\n", month, day, year, dayOfWeekString);
    
            System.out.println("\nNow, let's calculate the number of leap years within a year range.");
            System.out.println("Enter the starting year:");
            int startYear = scanner.nextInt();
            System.out.println("Enter the ending year:");
            int endYear = scanner.nextInt();
    
            int leapYears = numberOfLeapYears(startYear, endYear);
            System.out.printf("There are %d leap years between %d and %d.\n", leapYears, startYear, endYear);
    
            scanner.close();
        }
}
CalendarApp.main(null);
Welcome to CalendarApp!
Please enter a date (MM DD YYYY):
The date 01/02/2025 falls on a Thursday.

Now, let's calculate the number of leap years within a year range.
Enter the starting year:
Enter the ending year:
There are 9519 leap years between 11 and 39263.

This question involves the implementation of a fitness tracking system that is represented by the StepTracker class. A StepTracker object is created with a parameter that defines the minimum number of steps that must be taken for a day to be considered active.

The StepTracker class provides a constructor and the following methods.

  • addDailySteps, which accumulates information about steps, in readings taken once per day
  • activeDays, which returns the number of active days
  • averageSteps, which returns the average number of steps per day, calculated by dividing the total number of steps taken by the number of days tracked

Write the complete StepTracker class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class StepTrackerApp {
    public static void main(String[] args) {
        StepTracker stepTracker = new StepTracker(10000);

        stepTracker.addDailySteps(9000);
        stepTracker.addDailySteps(5000);
        stepTracker.addDailySteps(13000);
        stepTracker.addDailySteps(23000);
        stepTracker.addDailySteps(1111);

        stepTracker.displaySummary();
    }
}

class StepTracker {
    private int minSteps;
    private int totalSteps;
    private int totalDays;

    // Constructor
    public StepTracker(int minSteps) {
        this.minSteps = minSteps;
        this.totalSteps = 0;
        this.totalDays = 0;
    }

    // Method to add daily steps
    public void addDailySteps(int steps) {
        totalSteps += steps;
        totalDays++;
    }

    // Method to calculate the number of active days
    public int activeDays() {
        int activeDays = 0;
        if (totalDays > 0) {
            if (totalSteps >= minSteps) {
                activeDays = totalDays;
            }
        }
        return activeDays;
    }

    // Method to calculate the average steps per day
    public double averageSteps() {
        if (totalDays == 0) {
            return 0.0;
        } else {
            return (double) totalSteps / totalDays;
        }
    }

    public void reset() {
        totalSteps = 0;
        totalDays = 0;
    }

    public int getMinSteps() {
        return minSteps;
    }

    public void setMinSteps(int minSteps) {
        this.minSteps = minSteps;
    }

    public void displaySummary() {
        System.out.println("StepTracker Summary:");
        System.out.println("Minimum steps for a day to be considered active: " + minSteps);
        System.out.println("Total steps recorded: " + totalSteps);
        System.out.println("Total days tracked: " + totalDays);
        System.out.println("Number of active days: " + activeDays());
        System.out.println("Average steps per day: " + averageSteps());
    }
}
StepTrackerApp.main(null);
StepTracker Summary:
Minimum steps for a day to be considered active: 10000
Total steps recorded: 51111
Total days tracked: 5
Number of active days: 5
Average steps per day: 10222.2