Question: 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
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.

Listing



Zeller's congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is 26(m + 1) 5j % 7 10 Where h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6: Friday). q is the day of the month. im is the month (3: March, 4: April, . 12: December). January and February are counted as months 13 and 14 of the previous year. year j is the century (i.e., 100 k is the year of the century (i.e., year % 100). Note that the division in the formula performs an integer division. Write a program that prompts the user to enter a year, month, and day of the month, and displays the name of the day of the week. Here are some sample runs: Enter year: (e.g., 2012): 2015 |Jtter Enter month: 1-12: 1 Ferte Enter the day of the month: 1-31: 25 utte Day of the week is Sunday Enter year: (e.g., 2012): 2012 ter Enter month: 1-12: 5 -Enter Enter the day of the month: 1-31: 12 Jenter Day of the week is Saturday 1 import java.util.Scanner; 3 public class PrintCalendar { /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2012): "); int year = input.nextInt(); 10 11 // Prompt the user to enter month System.out.print("Enter month as a number between 1 and 12: "); int month = input.nextInt(); 12 13 // Print calendar for the month of the year printMonth (year, month); 16 17 18 19 /** Print the calendar for a month in a year */ public static void printMonth(int year, int month) { // Print the headings of the calendar printMonthTitle(year, month); 21 22 23 24 // Print the body of the calendar printMonthBody Cyear, month); 25 26 27 28 29 /** Print the month title, e.g., March 2012 */ public static void printMonthTitle(int year, int month) { System.out.println(" " + year); System.out.println("- System.out.printInc" Sun Mon Tue Wed Thu Fri Sat"): 30 + getMonthName(month) 31 32 33 --"); 34 35 36 37 /** Get the English name for the month */ public static String getMonthName (int month) { String monthName = switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = case 5: monthName = "May"; break; case 6: monthName case 7: monthName 38 39 40 41 42 "April"; break; 45 46 "June"; break; "July"; break; "August"; break; 47 48 49 50 case 8: monthName case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName - "November"; break; case 12: monthName = "December"; 51 52 53 54 55 56 57 58 return monthName; /** Print month body */ 111 111 222 222 222 233 333n33 m 444 444 45
Step by Step Solution
3.34 Rating (175 Votes )
There are 3 Steps involved in it
This program displays month in a calendar when year and month are given as input To get the start da... View full answer
Get step-by-step solutions from verified subject matter experts
