How to resolve the algorithm Five weekends step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Five weekends step by step in the Java programming language

Table of Contents

Problem Statement

The month of October in 2010 has five Fridays, five Saturdays, and five Sundays.

Algorithm suggestions

Extra credit Count and/or show all of the years which do not have at least one five-weekend month (there should be 29).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Five weekends step by step in the Java programming language

The provided Java program calculates the number of months from 1900 to 2100 that have five weekends (Saturdays and Sundays) and identifies the years that do not have any five-weekend months. A brief explanation of the code:

  1. Initialization:

    • years is a boolean array of size 201 (for years from 1900 to 2100). It tracks whether each year has at least one month with five weekends.
    • month31 is an array that represents months with 31 days (January, March, May, July, August, October, December).
  2. Loop Through Years and Months:

    • The outer loop iterates through the years from 1900 to 2100.
    • For each year, it iterates through the months in month31, as these months have 31 days, which is important for determining if a month has five weekends.
  3. Calendar Calculations:

    • For each month, it creates a Calendar instance to represent the first day of that month in the current year.
    • It then checks if the first day of the month is a Friday. If it is, it means that the month has five weekends because it starts and ends on weekends.
  4. Tracking Five-Weekend Months:

    • If the first day of the month is a Friday, it sets the corresponding element in the years array to true to indicate that the month has five weekends.
    • It also increments the numMonths counter, which keeps track of the total number of months with five weekends.
    • The months that have five weekends are appended to the months StringBuilder.
  5. Printing Results:

    • After examining all the months and years, it prints the number of months with five weekends and the list of months that meet this criterion.
    • It also prints a list of years that do not have any five-weekend months by checking the years array.

In summary, this program comprehensively calculates and displays information about months and years that have a specific number of weekends. It uses the Calendar class to determine the day of the week for specific dates and track months with five weekends.

Source code in the java programming language

import java.util.Calendar;
import java.util.GregorianCalendar;

public class FiveFSS {
    private static boolean[] years = new boolean[201];
    private static int[] month31 = {Calendar.JANUARY, Calendar.MARCH, Calendar.MAY,
        Calendar.JULY, Calendar.AUGUST, Calendar.OCTOBER, Calendar.DECEMBER};

    public static void main(String[] args) {
        StringBuilder months = new StringBuilder();
        int numMonths = 0;
        for (int year = 1900; year <= 2100; year++) {
            for (int month : month31) {
                Calendar date = new GregorianCalendar(year, month, 1);
                if (date.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
                    years[year - 1900] = true;
                    numMonths++;
                    //months are 0-indexed in Calendar
                    months.append((date.get(Calendar.MONTH) + 1) + "-" + year +"\n");
                }
            }
        }
        System.out.println("There are "+numMonths+" months with five weekends from 1900 through 2100:");
        System.out.println(months);
        System.out.println("Years with no five-weekend months:");
        for (int year = 1900; year <= 2100; year++) {
            if(!years[year - 1900]){
                System.out.println(year);
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Increment a numerical string step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the J programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Emacs Lisp programming language