****PLEASE MAKE SURE THE CODE FOR THIS RUNS IN NETBEANS JAVA SE**** COMPREHENSIV
ID: 3796523 • Letter: #
Question
****PLEASE MAKE SURE THE CODE FOR THIS RUNS IN NETBEANS JAVA SE****
COMPREHENSIVE
Zeller's congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The formula is:
h = (q + 26(m+1)/10 + k + k/4 + j/4 + 5j) % 7
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.
m is the month (3: March, 4: April, ..., 12: December). January and February are counted as months 13 and 14 of the previous year.
j is the century (i.e., year/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
Enter month: 1-12: 1
Enter day of the month: 1-31: 25
Day of the week is Sunday.
Explanation / Answer
DayOfTheWeek.java
import java.util.Scanner;
public class DayOfTheWeek {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a year, month and a day
System.out.print("Enter year (e.g., 2012): ");
int year = input.nextInt();
System.out.println();
System.out.print("Enter month: 1-12: ");
int month = input.nextInt();
System.out.println();
System.out.print("Enter the day of the month: 1-31: ");
int day = input.nextInt();
System.out.println();
// Check if the month is January or February
// If the month is January and February, convert to 13, and 14,
// and year has to -1. (Go to previous year).
if (month == 1 || month == 2) {
month += 12;
year--;
}
// Compute the answer
int k = year % 100; // The year of the century
int j = (int)(year / 100.0); // the century
int q = day;
int m = month;
int h = (q + (int)((26 * (m + 1)) / 10.0) + k + (int)(k / 4.0)
+ (int)(j / 4.0) + (5 * j)) % 7;
String result = "Day of the week is ";
//Display the name of the day of the week
if (h == 0)
System.out.print(result + "Saturday");
else if (h == 1)
System.out.print(result + "Sunday");
else if (h == 2)
System.out.print(result + "Monday");
else if (h == 3)
System.out.print(result + "Tuesday");
else if (h == 4)
System.out.print(result + "Wednesday");
else if (h == 5)
System.out.print(result + "Thursday");
else
System.out.print(result + "Friday");
}
}
Output:-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.