Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit97692d5

Browse files
committed
Added exercise 6.34 (Print Calculator adapted)
1 parentdba8034 commit97692d5

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
2.57 KB
Binary file not shown.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
importjava.util.Scanner;
2+
3+
/*
4+
Adapted by Sleekpanther from SquirrelCoder's initial idea from 29-Jan-17.
5+
Uses arrays, even though they technically aren't introduced until chapter 7. But it simplifies this a lot
6+
7+
6.34 (Print calendar) Programming Exercise 3.21 uses Zeller's congruence to calculate
8+
the day of the week. Simplify Listing 6.12, PrintCalendar.java, using Zeller's
9+
algorithm to get the start day of the month.
10+
11+
12+
Exercise 3.21 details
13+
(Science: day of the week) Zeller's congruence is an algorithm developed by
14+
Christian Zeller to calculate the day of the week. The formula is:
15+
h=(q + (26(m+1))/10 + k + k/4 + j/4 +5*j)%7
16+
h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday).
17+
q is the day of the month.
18+
m is the month (3: March, 4: April, ..., 12: December). January and February are counted as months 13 and 14 of the previous year.
19+
j is the century (i.e. year/100)
20+
k is the year of the century (i.e., year % 100).
21+
22+
Note that the division in the formula performs an integer division. Write a program
23+
that prompts the user to enter a year, month, and day of the month, and
24+
displays the name of the day of the week.
25+
(Hint: January and February are counted as 13 and 14 in the formula, so you need
26+
to convert the user input 1 to 13 and 2 to 14 for the month and change the year to the previous year.)
27+
*/
28+
publicclassPrintCalendar {
29+
publicstaticvoidmain(String[]args) {
30+
Scannerkeyboard =newScanner(System.in);
31+
32+
// get & validate user year
33+
System.out.print("Enter full year (e.g., 2012): ");
34+
intyear =keyboard.nextInt();
35+
while (!isValidYear(year)) {//validate input
36+
System.out.println("Invalid Year!");
37+
System.out.print("Enter full year (e.g., 2012): ");
38+
year =keyboard.nextInt();
39+
}
40+
41+
// get & validate user month
42+
System.out.print("Enter month as number between 1 and 12: ");
43+
intmonth =keyboard.nextInt();
44+
while (!isValidMonth(month)) {//validate input
45+
System.out.println("Invalid Month!");
46+
System.out.print("Enter month as number between 1 and 12: ");
47+
month =keyboard.nextInt();
48+
}
49+
50+
51+
printCalendarHeader(month,year);// print the calendar header
52+
53+
printFirstDay(month,year);// print the calendar first day
54+
55+
printCalendarItself(month,year);// print the calendar itself
56+
}
57+
58+
publicstaticbooleanisValidYear(intyear) {
59+
returnyear >0;//might want to check an upper bound, not sure if this formula works for HUGE numbers
60+
}
61+
62+
publicstaticbooleanisValidMonth(intmonth) {
63+
returnmonth >0 &&month <=12;
64+
}
65+
66+
publicstaticvoidprintCalendarHeader(intmonth,intyear) {
67+
String[]months = {"January","February","March","April","May","June","July","August","Septemter","October","November","December"};
68+
69+
System.out.print("\t\t"+months[month-1]+"\t");//access the month array with a -1 offset since arrays count from 0
70+
System.out.println(year);
71+
System.out.println("---------------------------");
72+
73+
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
74+
}
75+
76+
publicstaticvoidprintFirstDay(intmonth,intyear) {
77+
intfirstDay =dayOfWeek(1,month,year);//calculate the 1st day
78+
79+
StringleadingTabs ="1";//Holds any leading tabs to align 1st row of numbers in a calendar. This takes care of firstDay=1
80+
81+
//cases for firstDay between 2 & 6 (adds a "\t" at the beginning of the string each iteration)
82+
//Loop starts from 1 since we want 1 less tab than the value of firstDay (firstDay=1 is 0 tabs, firstDay=2 is 1 tab, firstDay=3 is 2 tabs, firstDay=4 is 3 tabs, firstDay=5 is 4 tabs)
83+
for(inti =1;i<firstDay;i++){
84+
leadingTabs ="\t" +leadingTabs;
85+
}
86+
if(firstDay ==0){//reset it & ignore what the loop did if it's 0. THIS IS A SPECIAL CASE. We want 6 tabs
87+
leadingTabs ="\t\t\t\t\t\t1";
88+
}
89+
90+
System.out.print(leadingTabs +"\t");
91+
}
92+
93+
publicstaticvoidprintCalendarItself(intmonth,intyear) {
94+
// find out the last day of that month
95+
// whether it's 28/29/30/31 days
96+
intlastDayOfMonth =lastDayOfMonth(month,year);
97+
98+
// print the calendar itself
99+
for (inti =2;i <=lastDayOfMonth;i++) {
100+
intprintedDay =dayOfWeek(i,month,year);
101+
if (printedDay ==1) {
102+
System.out.println();
103+
}
104+
System.out.print(i +"\t");
105+
}
106+
}
107+
108+
//Implement Zeller's Algorithm
109+
publicstaticintdayOfWeek(intdayOfMonth,intmonth,intyear) {
110+
if (month ==1 ||month ==2) {
111+
month =month +12;
112+
year--;
113+
}
114+
intq,m,j,k;
115+
q =dayOfMonth;
116+
m =month;//adjusted month (corrected for January & February being 13 & 14 respectively)
117+
j =year/100;//century
118+
k =year%100;//year of the century
119+
intdayOfTheWeek = (q + (26*(m+1) /10) +k +k/4 +j/4 + (5*j)) %7;//performs integer division where appropriate (like the Algorithms wants)
120+
returndayOfTheWeek;
121+
}
122+
123+
publicstaticbooleanisLeapYear(intyear) {
124+
returnyear %400 ==0 || (year %4 ==0 &&year %100 !=0);
125+
}
126+
127+
publicstaticintlastDayOfMonth(intmonth,intyear) {
128+
intlastDayOfMonth;
129+
if (month ==1 ||month ==3 ||month ==5 ||month ==7 ||month ==8 ||month ==10 ||month ==12) {
130+
lastDayOfMonth =31;
131+
}elseif (month ==2) {
132+
if (isLeapYear(year)) {
133+
lastDayOfMonth =29;
134+
}else {
135+
lastDayOfMonth =28;
136+
}
137+
}else {
138+
lastDayOfMonth =30;
139+
}
140+
returnlastDayOfMonth;
141+
}
142+
143+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp