Skip to content

Commit 7ec8ff4

Browse files
finished exercise 28
1 parent c4004f6 commit 7ec8ff4

File tree

1 file changed

+116
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)