Programming 5. Calendar and Java Methods
People often want to know what day a day is or was. In this project, you are to develop a program to solve the problem. The program should repeatedly determine and print out the week day of the day that the user gives.
Your program must satisfy the following requirements:
- Any date a user enters must be in the format of
MM/DD/CCYY
whereMM
are two digits representing the month,DD
are two digits representing the day in the month, andCCYY
are four digits representing the year. - The output should be formatted as this example:
February 3, 2023 is Friday
if the date given (i.e.,02/03/2023
in this example) is in the future orJanuary 1, 2022 is Saturday
if the date given (i.e.,01/01/2022
in this example) was in the past. - The program should first prompt the user to enter today’s date.
- The program should then prompt the user to enter today’s day of week. Here 0 is Sunday, 1 is Monday, 2 is Tuesday, and so on so forth.
- The program then repeatedly doing the following:
- Prompt the user to enter a date
- Determine the day of week of the date
- Print out the day of week of the date
- Break out of this loop when the user enters
01/01/0000
.
- The program should be robust against any rogue user inputs.
- All user inputs are considered as an input a line.
This program is challenging. You are expected to apply the “divide-and-conque” methodology using Java method to solve the program. The figure below shows the required break-down of the program. You should first complete several essential methods, as indicated below, and use them to complete your final program.
public static void main(String[] args)
public static String readFromUser(Scanner scanner, String prompt, int inputType)
public static void printDayOfWeek(Scanner scanner, String today, String todayDayOfWeek)
public static boolean isValidInput(String input, int inputType)
public static int getNumberOfDaysInMonth(int year, int month)
public static boolean isLeapYear(int year)
public static int getTotalNumberOfDays(int year, int month, int day, int refYear, int refMonth, int refDay)
public static int getTotalNumberOfDaysAhead(int yearFuture, int monthFuture, int dayFuture, int year, int month, int day)
public static int getDateField(String date, char type)
public static int computeDayOfWeek(int daysApart, int refWeekDay)
public static String getMonthName(int month)
public static String getDayName(int day)
The following is a sample run of the program:
$ java DayOfWeek
Enter today's date (MM/DD/CCYY): 10/18/2022
Enter today's day of week (0 - 6): 2
Enter a date (MM/DD/CCYY): 11/09/2022
November 9, 2022 is Wednesday
Enter a date (MM/DD/CCYY): 11/10/2022
November 10, 2022 is Thursday
Enter a date (MM/DD/CCYY): 11/07/2022
November 7, 2022 is Monday
Enter a date (MM/DD/CCYY): 12/31/2022
December 31, 2022 is Saturday
Enter a date (MM/DD/CCYY): 01/01/2023
January 1, 2023 is Sunday
Enter a date (MM/DD/CCYY): 01/01/2022
January 1, 2022 is Saturday
Enter a date (MM/DD/CCYY): 03/04/2000
March 4, 2000 is Saturday
Enter a date (MM/DD/CCYY): 02/29/2400
February 29, 2400 is Tuesday
Enter a date (MM/DD/CCYY): 02/28/2400
February 28, 2400 is Monday
Enter a date (MM/DD/CCYY): 02/29/2100
Enter a date (MM/DD/CCYY): 02/28/2100
February 28, 2100 is Sunday
Enter a date (MM/DD/CCYY): 02/29/2004
February 29, 2004 is Sunday
Enter a date (MM/DD/CCYY): 02/29/2003
Enter a date (MM/DD/CCYY): 02/28/2003
February 28, 2003 is Friday
Enter a date (MM/DD/CCYY): 01/01/0000
Explanation of the Methods
While the supposed functionalities of some methods are obvious from the method names, those of the others are not. Below are some explanation about the design of these methods.
public static String readFromUser(Scanner scanner, String prompt, int inputType)
This method uses a Scanner
to read the user’s input. There are two types of inputs in the program, date
and day of week. The method shall repeatedly prompt the user with the prompt message in prompt
until
the user enters a valid input for the given type of input in inputType
.
public static void printDayOfWeek(Scanner scanner, String refDate, String refDayOfWeek)
This method shall repeatedly prompt the user to enter a date, determine the day of week
of the date, and print the determination. To determine the day of week of the date entered
by the user, the method uses a given reference date in refDate
and its day of week
in refDayOfWeek
.
public static boolean isValidInput(String input, int inputType)
The method checks if the input is valid for the given inputType
.
public static int getNumberOfDaysInMonth(int year, int month)
This method determines the number of days in the month
of the year
.
public static boolean isLeapYear(int year)
This method checks if year
is a leap year.
public static int getTotalNumberOfDays(int year, int month, int day, int refYear, int refMonth, int refDay)
This method computes the number of days the date represented by year
, month
, and day
between the reference
date represented by refYear
, refMonth
, and refDay
. If the date represented by year
, month
, and day
is
in the future when compared to the reference
date represented by refYear
, refMonth
, and refDay
, the method returns a positive integer; otherwise, it
returns a negative integer indicating the date represented by year
, month
, and day
is
in the past.
public static int getTotalNumberOfDaysAhead(int yearFuture, int monthFuture, int dayFuture, int year, int month, int day)
This method returns how many days a future date is ahead of another date.
public static int getDateField(String date, char type)
This method returns either the month, or the day, or the year in the date
that is in the format of MM/DD/CCYY
where
MM
is the month, DD
the day, and CCYY
the year.
public static int computeDayOfWeek(int daysApart, int refWeekDay)
This method compares the day of week. Note that daysApart
is the return value of getTotalNumberOfDays
, and it can
be either positive, negative, or 0.
public static String getMonthName(int month)
This method returns the month name like January, February, and so on.
public static String getDayName(int day)
This method returns the day name like Sunday, Monday, and so on.