Skip to content

Commit 3ce63be

Browse files
finished exercise 29
1 parent beaf0e8 commit 3ce63be

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* @author : Jonathan Birkey
3+
* @mailto : [email protected]
4+
* @created : 28Feb2024
5+
* <p>(Display calendars) Write a program that prompts the user to enter the year and first day
6+
* of the year and displays the calendar table for the year on the console. For example, if the
7+
* user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display
8+
* the calendar for each month in the year, as follows:
9+
* <p>January 2013
10+
* <p>---------------------------
11+
* <p>Sun Mon Tue Wed Thu Fri Sat
12+
* <p>1 2 3 4 5
13+
* <p>6 7 8 9 10 11 12
14+
* <p>13 14 15 16 17 18 19
15+
* <p>20 21 22 23 24 25 26
16+
* <p>27 28 29 30 31
17+
* <p>
18+
* <p>...
19+
* <p>
20+
* <p>December 2013
21+
* <p>---------------------------
22+
* <p>Sun Mon Tue Wed Thu Fri Sat
23+
* <p>1 2 3 4 5 6 7
24+
* <p>8 9 10 11 12 13 14
25+
* <p>15 16 17 18 19 20 21
26+
* <p>22 23 24 25 26 27 28
27+
* <p>29 30 31
28+
*/
29+
package com.github.jonathanbirkey.chapter05;
30+
31+
import java.util.Scanner;
32+
33+
public class Exercise29 {
34+
public static void main(String[] args) {
35+
Scanner input = new Scanner(System.in);
36+
System.out.print("Enter year: (e.g., 2012): ");
37+
int year = input.nextInt();
38+
System.out.print("Enter first day of year (0 - Saturday, 1 - Sunday, 2 - Monday, etc): ");
39+
int firstDay = input.nextInt();
40+
input.close();
41+
42+
int daysInMonth = 0;
43+
String monthHeader;
44+
45+
for (int month = 1; month <= 12; month++) {
46+
if (month == 1
47+
|| month == 3
48+
|| month == 5
49+
|| month == 7
50+
|| month == 8
51+
|| month == 10
52+
|| month == 12) {
53+
daysInMonth = 31;
54+
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
55+
daysInMonth = 30;
56+
} else {
57+
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
58+
daysInMonth = 29;
59+
} else {
60+
daysInMonth = 28;
61+
}
62+
}
63+
switch (month) {
64+
case 1:
65+
monthHeader = " January " + year;
66+
break;
67+
case 2:
68+
monthHeader = " Febuary " + year;
69+
break;
70+
case 3:
71+
monthHeader = " March " + year;
72+
break;
73+
case 4:
74+
monthHeader = " April " + year;
75+
break;
76+
case 5:
77+
monthHeader = " May " + year;
78+
break;
79+
case 6:
80+
monthHeader = " June " + year;
81+
break;
82+
case 7:
83+
monthHeader = " July " + year;
84+
break;
85+
case 8:
86+
monthHeader = " August " + year;
87+
break;
88+
case 9:
89+
monthHeader = " September " + year;
90+
break;
91+
case 10:
92+
monthHeader = " October " + year;
93+
break;
94+
case 11:
95+
monthHeader = " November " + year;
96+
break;
97+
case 12:
98+
monthHeader = " December " + year;
99+
break;
100+
default:
101+
monthHeader = "Invalid";
102+
}
103+
104+
System.out.printf(
105+
"%s\n---------------------------\nSun Mon Tue Wed Thu Fri Sat\n", monthHeader);
106+
107+
int dayShift = 0;
108+
if (firstDay > 0) {
109+
dayShift = firstDay - 1;
110+
} else {
111+
dayShift = firstDay + 6;
112+
}
113+
114+
for (int i = 0; i < dayShift; i++) {
115+
System.out.print(" ");
116+
}
117+
118+
for (int date = 1; date <= daysInMonth; date++) {
119+
if (dayShift == 6) {
120+
if (date < 10) {
121+
System.out.printf(" %d\n", date);
122+
} else {
123+
System.out.printf(" %d\n", date);
124+
}
125+
dayShift = 0;
126+
} else {
127+
if (date < 10) {
128+
System.out.printf(" %d ", date);
129+
} else {
130+
System.out.printf(" %d ", date);
131+
}
132+
dayShift++;
133+
}
134+
}
135+
System.out.print("\n\n");
136+
137+
firstDay = (firstDay + daysInMonth) % 7;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)