In: Computer Science
how to correct this java code so that i get the correct day of week?
and test the year
public static void main(String[] args) {
//
Scanner s = new Scanner(System.in);
//needed info
//year month, day
int year, month, dayOfMonth;
// add day of week , century yr
int dayOfWeek, century, yearOfCentury;
//user inputs year
System.out.print("Enter year: (example, 2020):");
year = s.nextInt();
//user inputs month by number
System.out.print("Enter month: 1-12:");
month = s.nextInt();
//user inputs day of month
System.out.print("Enter the day of the month: 1-31:");
dayOfMonth = s.nextInt();
// testing if month is January or February
if(month == 1) {
month = 13;
} else if(month == 2) {
month = 14;
}
//determining year month
century = year / 100;
yearOfCentury = year % 100;
dayOfWeek = (dayOfMonth + ((26 * (month + 1))/10) + yearOfCentury +
yearOfCentury/4 + century/4 + century * 5) % 7;
//figuring out day
if(dayOfWeek == 0) {
System.out.print("Day of the week is Saturday.");
}
else if(dayOfWeek == 1) {
System.out.print("Day of the week is Sunday.");
}
else if(dayOfWeek == 2) {
System.out.print("Day of the week is Monday.");
}
else if(dayOfWeek == 3) {
System.out.print("Day of the week is Tuesday.");
}
else if(dayOfWeek == 4) {
System.out.print("Day of the week is Wednesday.");
}
else if(dayOfWeek == 5) {
System.out.print("Day of the week is Thursday.");
}
else if(dayOfWeek == 6) {
System.out.print("Day of the week is Friday.");
}
}
}
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
DayOfWeekDebug.java
package classes15;
import java.util.Scanner;
public class DayOfWeekDebug {
public static void main(String[] args) {
//
Scanner s = new
Scanner(System.in);
//needed info
//year month, day
int year, month, dayOfMonth;
// add day of week , century
yr
int dayOfWeek, century,
yearOfCentury;
//user inputs year
System.out.print("Enter year:
(example, 2020):");
year = s.nextInt();
if(year<0) {
System.out.println("Error!!! Invalid year is entered.");
return;
}
//user inputs month by
number
System.out.print("Enter month:
1-12:");
month = s.nextInt();
//user inputs day of month
System.out.print("Enter the day of
the month: 1-31:");
dayOfMonth = s.nextInt();
// testing if month is January
or February
if(month == 1) {
month =
13;
} else if(month == 2) {
month =
14;
}
//determining year month
century = year / 100;
yearOfCentury = year % 100;
dayOfWeek = (dayOfMonth + ((26 *
(month + 1))/10) + yearOfCentury + yearOfCentury/4 + century/4 +
century * 5) % 7;
//figuring out day
if(dayOfWeek == 0) {
System.out.print("Day of the week is Saturday.");
}
else if(dayOfWeek == 1) {
System.out.print("Day of the week is Sunday.");
}
else if(dayOfWeek == 2) {
System.out.print("Day of the week is Monday.");
}
else if(dayOfWeek == 3) {
System.out.print("Day of the week is Tuesday.");
}
else if(dayOfWeek == 4) {
System.out.print("Day of the week is Wednesday.");
}
else if(dayOfWeek == 5) {
System.out.print("Day of the week is Thursday.");
}
else if(dayOfWeek == 6) {
System.out.print("Day of the week is Friday.");
}
}
}