How to resolve the algorithm Last Friday of each month step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Last Friday of each month step by step in the Kotlin programming language

Table of Contents

Problem Statement

Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).

Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Kotlin programming language

Explanation:

1. Program Outline:

  • This Kotlin program calculates and prints the last Friday of each month in a given year.

2. User Input:

  • It prompts the user to enter a year, which is stored in the variable year.

3. Initialization:

  • It creates a GregorianCalendar object (calendar) with the specified year, month (set to January, i.e., 0), and day of month (set to 31). This is a placeholder date to facilitate month traversal and calculation.

4. Loop Through Months:

  • The program then iterates through the months from January to December (months 1 to 12):
    • For each month, it calculates the number of days in the month using getActualMaximum(Calendar.DAY_OF_MONTH).

5. Calculating the Last Friday:

  • It computes the offset from the last day of the month (daysInMonth) to the nearest previous Friday using calendar[Calendar.DAY_OF_WEEK] - Calendar.FRIDAY. If the offset is negative, it adds 7 to account for days in the previous week.
  • The last Friday of the month is calculated as daysInMonth - offset.

6. Output:

  • The program prints the last Friday of each month in the format "YYYY-MM-DD", where YYYY is the year, MM is the month (formatted with leading zeros), and DD is the day of the month (also formatted with leading zeros).

7. Month Traversal:

  • After processing each month, the calendar object is advanced to the first day of the next month by adding 1 to Calendar.DAY_OF_MONTH and Calendar.MONTH. It then subtracts 1 from Calendar.DAY_OF_MONTH to go back to the last day of the month.

Example Output: For the year 2023, the output would be:

The last Fridays of each month in 2023 are as follows:
2023-01-27
2023-02-24
2023-03-31
2023-04-28
2023-05-26
2023-06-30
2023-07-28
2023-08-25
2023-09-29
2023-10-27
2023-11-24
2023-12-29

Source code in the kotlin programming language

// version 1.0.6

import java.util.*

fun main(args: Array<String>) {
    print("Enter a year : ")
    val year = readLine()!!.toInt()
    
    println("The last Fridays of each month in $year are as follows:")
    val calendar = GregorianCalendar(year, 0, 31)
    for (month in 1..12) {
        val daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        var offset = calendar[Calendar.DAY_OF_WEEK] - Calendar.FRIDAY
        if (offset < 0) offset += 7
        val lastFriday = daysInMonth - offset
        println("$year-" + "%02d-".format(month) + "%02d".format(lastFriday))
        if (month < 12) {
            calendar.add(Calendar.DAY_OF_MONTH, 1)
            calendar.add(Calendar.MONTH, 1)
            calendar.add(Calendar.DAY_OF_MONTH, -1)
        }
    }
}


  

You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Clojure programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Scala programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the Go programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the JavaScript programming language