This question is based on Java Programming language. The below java code, displays the calendar for a given month of the year. The program prompts the user to enter the year and the month, then displays the entire calendar for the month. Can you explain the code and its functions ? you can explain in paragraph or point form.   //Java code // Header file to take user input import java.util.Scanner; //main class public class Main { //method is used to check days for the month public static int dayOfMonth(int mon, int day, int yr) { int y = yr - (14 - mon) / 12; int x = y + y/4 - y/100 + y/400; int m = mon + 12 * ((14 - mon) / 12) - 2; int d = (day + x + (31*m)/12) % 7; return d; //return day of month }   //function is used to check whether the year is leap year or not public static boolean leapYear(int yr) { //check using if-else if ((yr % 4 == 0) && (yr % 100 != 0)) return true; //return true if not a leapYear if (yr % 400 == 0) return true; //return true if not a leapYear return false; //return false if not a leapYear }   //main method public static void main(String[] args) { Scanner in = new Scanner(System.in); //ask for user input System.out.print("Enter full year (e.g., 2022): "); int yr = in.nextInt(); //user input for year System.out.print("Enter month in number between 1 and 12: "); int mon = in.nextInt(); //user input for month //name of each month, first is blank so that january is in the first index String[] month = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};   // number of days present in each month int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   // checking for leap year if (mon == 2 && leapYear(yr)) days[mon] = 29;   // month name and days name System.out.println(" " + month[mon] + " " + yr); System.out.println("----------------------------–"); System.out.println("Sun Mon Tue Wed Thu Fri Sat"); // first day int d = dayOfMonth(mon, 1, yr);   // printing the calendar for (int i = 0; i < d; i++) System.out.print(" "); for (int i = 1; i <= days[mon]; i++) { System.out.printf("%3d ", i); if (((i + d) % 7 == 0) || (i == days[mon])) System.out.println(); }   } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

This question is based on Java Programming language.

The below java code, displays the calendar for a given month of the year. The program prompts the user to enter the year and the month, then displays the entire calendar for the month. Can you explain the code and its functions ? you can explain in paragraph or point form.

 

//Java code

// Header file to take user input

import java.util.Scanner;

//main class

public class Main

{

//method is used to check days for the month

public static int dayOfMonth(int mon, int day, int yr) {

int y = yr - (14 - mon) / 12;

int x = y + y/4 - y/100 + y/400;

int m = mon + 12 * ((14 - mon) / 12) - 2;

int d = (day + x + (31*m)/12) % 7;

return d; //return day of month

}

 

//function is used to check whether the year is leap year or not

public static boolean leapYear(int yr) {

//check using if-else

if ((yr % 4 == 0) && (yr % 100 != 0))

return true; //return true if not a leapYear

if (yr % 400 == 0)

return true; //return true if not a leapYear

return false; //return false if not a leapYear

}

 

//main method

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//ask for user input

System.out.print("Enter full year (e.g., 2022): ");

int yr = in.nextInt(); //user input for year

System.out.print("Enter month in number between 1 and 12: ");

int mon = in.nextInt(); //user input for month

//name of each month, first is blank so that january is in the first index

String[] month = {"", "January", "February", "March", "April", "May", "June",

"July", "August", "September", "October", "November", "December"};

 

// number of days present in each month

int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

 

// checking for leap year

if (mon == 2 && leapYear(yr)) days[mon] = 29;

 

// month name and days name

System.out.println(" " + month[mon] + " " + yr);

System.out.println("----------------------------–");

System.out.println("Sun Mon Tue Wed Thu Fri Sat");

// first day

int d = dayOfMonth(mon, 1, yr);

 

// printing the calendar

for (int i = 0; i < d; i++)

System.out.print(" ");

for (int i = 1; i <= days[mon]; i++) {

System.out.printf("%3d ", i);

if (((i + d) % 7 == 0) || (i == days[mon])) System.out.println();

}

 

}

}

 

Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education