1. Engineering
  2. Computer Science
  3. write a static method with input parameters month day and...

Question: write a static method with input parameters month day and...

Question details

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.

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:

Day of
week is Thursday.
Day of week is Tuesday.

Also, 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.

Some tricky points about the Calendar class:

  1. A constructor is not used to instantiate a Calendar object. Use the static getInstance method instead:

    • Calendar cal = Calendar.getInstance( );

  2. 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.

  3. 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).

  4. 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:

  1. String[ ] dayNames = {"", "Sunday", "Monday", "Tuesday",
       "Wednesday", "Thursday", "Friday", "Saturday"};

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution