The portfolio task is to write a day of the week calculator which allows the user to input a date, and which calculates and outputs the day of the week on which that date fell (or will fall). Download Doomsday.java from Moodle. This is a class which implements John Horton Conway's 'doomsday' algorithm for calculating the day of the week for any date. The class has a static function
public static int getDayOfWeek( int day, int month, int year)
This returns -1 if the date entered is invalid. For valid dates, it returns 0 for Sunday, 1 for Monday, and so on. Call it like this:
int dayofWeek = Doomsday. getDayOfWeek( 29, 2, 2016 );
System.out.println( dayOfWeek );
// outputs '1' - 29/2/2016 was a Monday.
Your task is to write a GUI for this class. It should look something like the picture below. see image.
The program should include the following features:
Example outputs:
'Invalid input'
'45/32/2020 is not a valid date'
'29/2/2016 is a Monday'
Don't change anything in Doomsday.java. You should include it in your project, but you don't need to modify it.
Doomsday.java
public class Doomsday {
static int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int centuryDays[] = {5, 3, 2, 0};
static int doomsdays[] = {3, 28, 7, 4, 9, 6, 11, 8, 5, 10, 7, 12};
static boolean LeapYear(int year) {
return ((year % 400) != 0) && ((year % 4) == 0);
}
static boolean ValidDate(int day, int month, int year) {
if (year < 1583) {
return false; // Gregorian calendar started in 1582
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1) {
return false;
}
if (month == 2) {
if (LeapYear(year)) {
if (day > 29) {
return false;
}
} else if (day > 28) {
return false;
}
} else if (day > monthDays[month - 1]) {
return false;
}
return true;
}
public static int GetDayOfWeek(int day, int month, int year) {
// Get the day of the week for any Gregorian date using Conway's doomsday algorithm
// input - calendar day, calendar month (Jan = 1, Feb = 2...), calendar year.
// return: day of week as 0 (Sun), 1 (Mon) ... 6 (Sat), -1 (invalid date)
if (!ValidDate(day, month, year)) {
return -1;
}
// valid date, we are good to go.
int centuryDay = centuryDays[((year / 100) - 14) % 4];
boolean isLeapYear = LeapYear(year);
year = year % 100; // from now on we use year in the century.
int anchorDay = year / 12 + (year % 12) + (year % 12) / 4;
int doomsday = doomsdays[month - 1];
if (isLeapYear && month < 3) {
doomsday++;
}
// the +35 ensures a positive number before taking the modulus (and 35%7 = 0)
return (centuryDay + anchorDay + day - doomsday + 35) % 7;
}
}