**6.34 (Printing Calendar) Programming Practice Problem 3.21 uses the Zeller consistency principle to calculate the day of the week. Use Zeller's algorithm to simplify the program Listing 6-12 to get the day of the week when the first day of each month starts. **6.34(Print calendar) Programming Exercise 3.21 uses Zeller's congruence to calculate the day of the week. Simplify Listing 6.12, PrintCalendar.java, using Zeller's algorithm to get the start day of the month.
Reference Code:
package chapter06;import java.util.Scanner;publicclassCode_34{
publicstaticvoidmain(String[] args){
Scanner input =newScanner(System.in);
System.out.print("Enter full year (e.g., 2012): ");int year = input.nextInt();
System.out.print("Enter month as a number between 1 and 12: ");int month = input.nextInt();printMonth(year, month);}publicstaticvoidprintMonth(int year,int month){
printMonthTitle(year, month);printMonthBody(year, month);}publicstaticvoidprintMonthTitle(int year,int month){
System.out.println(" "+getMonthName(month)+" "+ year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");}publicstatic String getMonthName(int month){
String monthName ="";switch(month){
case1:
monthName ="January";break;case2:
monthName ="February";break;case3:
monthName ="March";break;case4:
monthName ="April";break;case5:
monthName ="May";break;case6:
monthName ="June";break;case7:
monthName ="July";break;case8:
monthName ="August";break;case9:
monthName ="September";break;case10:
monthName ="October";break;case11:
monthName ="November";break;case12:
monthName ="December";}return monthName;}publicstaticvoidprintMonthBody(int year,int month){
int startDay =getStartDay(year, month);int numberOfDaysInMonth =getNumberOfDaysInMonth(year, month);int i =0;for(i =0; i < startDay; i++)
System.out.print(" ");for(i =1; i <= numberOfDaysInMonth; i++){
System.out.printf("%4d", i);if((i + startDay)%7==0)
System.out.println();}
System.out.println();}publicstaticintgetStartDay(int year,int month){
int day =1, h, q, m, j, k,statement;
q = day;if(month ==1|| month ==2){
month +=12;
year -=1;}
m = month;
j = year /100;
k = year %100;
h =(q +(26*(m +1)/10)+ k + k /4+ j /4+5* j)%7;switch(h){
case0:
statement =6;break;case1:
statement =0;break;case2:
statement =1;break;case3:
statement =2;break;case4:
statement =3;break;case5:
statement =4;break;case6:
statement =5;break;default:
statement =-1;}return statement;}publicstaticintgetNumberOfDaysInMonth(int year,int month){
if(month ==1|| month ==3|| month ==5|| month ==7|| month ==8|| month ==10|| month ==12)return31;if(month ==4|| month ==6|| month ==9|| month ==11)return30;if(month ==2)returnisLeapYear(year)?29:28;return0;}publicstaticbooleanisLeapYear(int year){
return year %400==0||(year %4==0&& year %100!=0);}}
The results show that:
Enter full year (e.g.,2012):2012
Enter month as a number between 1 and 12:2
February 2012-----------------------------
Sun Mon Tue Wed Thu Fri Sat
1234567891011121314151617181920212223242526272829
Process finished with exit code 0