In: Computer Science
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args)
Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen:
This program calculates the day of the week for any dates.
Enter month (1-12): 9
Enter day (1-31): 25
Enter year: 1998
The day of the week is Friday.
Hint: Use Zeller's congruence to compute the day of the week. Zeller's congruence relies on the following quantities:
J is the century (19, in our example)
K is the year within the century (98, in our example)
m is the month (9, in our example)
q is the day of the month (25, in our example)
The day of the week is determined by the following formula:
h = (q + 26(m + 1) / 10 + K + K / 4 + J / 4 + 5J) mod 7
where the results of the divisions are truncated. The value of h will lie between 0 (Saturday) and 6 (Friday).
Note: Zeller's congruence assumes that January and February are treated as months 13 and 14 of the previous year; this affects the values of K and m, and possibly the value of J. Note that the value of h does not match the desired output of the program, so some adjustment will be necessary. Apply Exception Handling.
Java code :
import java.util.*;
class Day_of_week {
static void finding_the_week(int day_value, int
month_value, int year_value){
if (month_value == 1) {
month_value =
13;
year_value--;
}
if (month_value == 2){
month_value =
14;
year_value--;
}
int q = day_value;
int m = month_value;
int K = year_value % 100;
int J = year_value / 100;
int h = (q + 26*(m + 1) / 10 + K +
K / 4 + J / 4 + 5*J) % 7;
switch (h)
{
case 0 :
System.out.println("Day of the week is Saturday"); break;
case 1 :
System.out.println("Day of the week is Sunday"); break;
case 2 :
System.out.println("Day of the week is Monday"); break;
case 3 :
System.out.println("Day of the week is Tuesdau"); break;
case 4 :
System.out.println("Day of the week is Wednesday"); break;
case 5 :
System.out.println("Day of the week is Thursday"); break;
case 6 :
System.out.println("Day of the week is Friday"); break;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int day,month,year;
System.out.print("Enter day
:");
day = sc.nextInt();
System.out.print("Enter month
:");
month = sc.nextInt();
System.out.print("Enter Year
:");
year = sc.nextInt();
finding_the_week(day,month,year);
}
}
Output :
please up vote.If any doubts comment below.Thank you