Problem 2 (Displaying calendar) Write a program that prompts the user to enter t
ID: 3870389 • Letter: P
Question
Problem 2 (Displaying calendar) Write a program that prompts the user to enter the year and first day of the year and displays on the console the calendar table for the year. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the calendar for each month in the year, as follows January 2005 Sun Mon Tue Wed Thu FriSat 10 11 12 13 1415 16 17 18 19 202122 23 24 25 26 272829 30 31 December 2005 Sun Mon Tue Wed Thu FriSat 10 11 12 13 14 151617 18 19 20 21 222324 25 26 27 28 2930 31 (Hint: the number of days in February for different year may be different, which depends on whether it is a leap year or plain year. A year is leap year if either (1) or (2) holds: (1) it is divisible by 400. (2) it is divisible by 4 but not divisible by 100Explanation / Answer
Calendar.java
import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
int getFirstDay;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the year: ");
int year = scanner.nextInt();
while (true) {
System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
getFirstDay = scanner.nextInt();
if (getFirstDay < 0 || getFirstDay > 6) {
System.out.println(":: Invalid Day.Enter Valid number between 0 and 6 ::");
continue;
} else
break;
}
for (int month = 1; month <= 12; month++) {
int days = 0;
String monthName = " ";
switch (month) {
case 1:
monthName = "January";
days = 31;
break;
case 2:
monthName = "February";
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
break;
case 3:
monthName = "March";
days = 31;
break;
case 4:
monthName = "April";
days = 30;
break;
case 5:
monthName = "May";
days = 31;
break;
case 6:
monthName = "June";
days = 30;
break;
case 7:
monthName = "July";
days = 31;
break;
case 8:
monthName = "August";
days = 31;
break;
case 9:
monthName = "September";
days = 30;
break;
case 10:
monthName = "October";
days = 31;
break;
case 11:
monthName = "November";
days = 30;
break;
case 12:
monthName = "December";
days = 31;
break;
default:
System.out.print("Invalid Month.");
System.exit(0);
break;
}
System.out.println(" " + monthName + " " + year);
System.out.println("__________________________________");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int i = 0;
int firstDay = 0;
switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 3;
break;
case 4:
firstDay = getFirstDay + 6;
break;
case 5:
firstDay = getFirstDay + 8;
break;
case 6:
firstDay = getFirstDay + 11;
break;
case 7:
firstDay = getFirstDay + 13;
break;
case 8:
firstDay = getFirstDay + 16;
break;
case 9:
firstDay = getFirstDay + 19;
break;
case 10:
firstDay = getFirstDay + 21;
break;
case 11:
firstDay = getFirstDay + 24;
break;
case 12:
firstDay = getFirstDay + 26;
break;
}
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
switch (month) {
case 1:
firstDay = getFirstDay;
break;
case 2:
firstDay = getFirstDay + 3;
break;
case 3:
firstDay = getFirstDay + 4;
break;
case 4:
firstDay = getFirstDay + 7;
break;
case 5:
firstDay = getFirstDay + 9;
break;
case 6:
firstDay = getFirstDay + 12;
break;
case 7:
firstDay = getFirstDay + 14;
break;
case 8:
firstDay = getFirstDay + 17;
break;
case 9:
firstDay = getFirstDay + 20;
break;
case 10:
firstDay = getFirstDay + 22;
break;
case 11:
firstDay = getFirstDay + 25;
break;
case 12:
firstDay = getFirstDay + 27;
break;
}
}
int dayOfWeek = 0;
if ((firstDay % 7) >= 0) {
if ((firstDay % 7) == 0) {
dayOfWeek = 0;
} else if ((firstDay % 7) == 1) {
dayOfWeek = 1;
System.out.print(" ");
} else if ((firstDay % 7) == 2) {
dayOfWeek = 2;
System.out.print(" ");
} else if ((firstDay % 7) == 3) {
dayOfWeek = 3;
System.out.print(" ");
} else if ((firstDay % 7) == 4) {
dayOfWeek = 4;
System.out.print(" ");
} else if ((firstDay % 7) == 5) {
dayOfWeek = 5;
System.out.print(" ");
} else if ((firstDay % 7) == 6) {
dayOfWeek = 6;
System.out.print(" ");
}
}
for (i = 1; i <= days; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + firstDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
}
}
______________________
Output:
Enter the year: 2005
Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 6
January 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
February 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
March 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
April 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
May 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
June 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
July 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
August 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
September 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
October 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
November 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
December 2005
__________________________________
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.