Question
Write a static method with input parameters month, day, and year that returns the day of the week in Java. The header should look like
Write a static method with input parameters month, day, and year that returns the day of the week in Java. The header should look like this:
public static String dayOfWeek(int month, int day, int year)
Use a Calendar class object to find the day of the week. This object must be defined in the dayOfWeek method, not in the main method. Also please write comments throughout or points will be taken off.
Important: don't use any date and/or time classes for this project other than Calendar. For example, don't use the SimpleDateFormat class. 50% deduction if you use date and/or time classes other than Calendar.
Some tricky points about the Calendar class:
-
A constructor is not used to instantiate a Calendar object. Use the static getInstance method instead:
-
Calendar cal = Calendar.getInstance( );
-
-
Use the Calendar instance method set to set the month, day, and year of your Calendar object, using the parameters passed in from the dayOfWeekmethod. Caution: the set method accepts its parameters in a different order than your getDayOfWeek method.
-
The int values representing the month are zero-based (0 means January, 1 means February, ... , 11 means December), but the days of the week are one-based (1 means Sunday, 2 means Monday, ..., 7 means Saturday).
-
Use the Calendar get method to get the day of week from the Calendar object.
The dayOfWeek method returns a String object. However, the Calendar object returns a zero-based int. Use this array in your dayOfWeek method to translate:
-
String[ ] dayNames = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Write three test classes:
-
A traditional test class named Test1 with a main method that tests your getDayOfWeek method using hardcoded parameters. Check at least five dates, including your birth date.
-
A JUnit test class named Test2 that tests the same dates that you tested in your traditional Test1 class.
-
An interactive class named Test3 that repeatedly prompts the user for a date and prints the corresponding day of week name, for example:
-
1/17/2019 Day of week is Thursday. 1/1/2019 Day of week is Tuesday.
-
Use a blank line to terminate your input, or use Control-Z for Windows, Control-D for Unix. See the SumOfProducts Example to see various ways of terminating an input loop.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started