I could not find your answer to Programming Exercise Chapter. 6 Number 34. so I thought I could share my answer :).
PS: Don't know it's efficient, or even a good implementation, but it works pretty good, for any given year.
PS2: I could not find the answer to question No. 33 too, and unfortunately I couldn't solve it yet, though I am working hard on it. When I have written the answer, I will share the code. :)
import java.util.Scanner;
/**
* Created by SquirrelCoder on 29-Jan-17.
*/
public class ZellerAlgorithm {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// getting user input
System.out.print("Enter full year (e.g., 2012): ");
int year = input.nextInt();
System.out.print("Enter month as number between 1 and 12: ");
int month = input.nextInt();
// check user input
if (!checkMonth(month)) {
System.exit(0);
}
// print the calendar header
printCalendarHeader(month, year);
// print the calendar first day
printFirstDay(month, year);
// print the calendar itself
printCalendarItself(month, year);
}
public static int weekDay(int day, int month, int year) {
if (month == 1 || month == 2) {
month = month + 12;
year--;
}
int q, m, k, j, h;
q = day;
m = month;
k = year % 100;
j = (int) (year / 100.0);
h = (q + (int) ((13 * (m + 1)) / 5.0) + k + (int) (k / 4.0) + (int) (j / 4.0) + (5 * j)) % 7;
return h;
}
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
public static void printCalendarHeader(int month, int year) {
switch (month) {
case 1:
System.out.print("\t\tJanuary\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 2:
System.out.print("\t\tFebruary\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 3:
System.out.print("\t\tMarch\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 4:
System.out.print("\t\tApril\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 5:
System.out.print("\t\tMay\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 6:
System.out.print("\t\tJune\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 7:
System.out.print("\t\tJuly\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 8:
System.out.print("\t\tAugust\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 9:
System.out.print("\t\tSeptember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 10:
System.out.print("\t\tOctober\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 11:
System.out.print("\t\tNovember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 12:
System.out.print("\t\tDecember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
}
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
}
public static int lastDayOfMonth(int month, int year) {
int lastDayOfMonth;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
lastDayOfMonth = 31;
} else if (month == 2) {
if (isLeapYear(year)) {
lastDayOfMonth = 29;
} else {
lastDayOfMonth = 28;
}
} else {
lastDayOfMonth = 30;
}
return lastDayOfMonth;
}
public static boolean checkMonth(int month) {
if (month > 12 || month < 0) {
System.out.println("Invalid Month!");
return false;
}
return true;
}
public static void printFirstDay(int month, int year) {
int firstDay = weekDay(1, month, year);
switch (firstDay) {
case 0:
System.out.print("\t\t\t\t\t\t1");
break;
case 1:
System.out.print("1");
break;
case 2:
System.out.print("\t1");
break;
case 3:
System.out.print("\t\t1");
break;
case 4:
System.out.print("\t\t\t1");
break;
case 5:
System.out.print("\t\t\t\t1");
break;
case 6:
System.out.print("\t\t\t\t\t1");
break;
}
System.out.print("\t");
}
public static void printCalendarItself(int month, int year) {
// find out the last day of that month
// whether it's 28/29/30/31 days
int lastDayOfMonth = lastDayOfMonth(month, year);
// print the calendar itself
for (int i = 2; i <= lastDayOfMonth; i++) {
int printedDay = weekDay(i, month, year);
if (printedDay == 1) {
System.out.println();
}
System.out.print(i + "\t");
}
}
}