Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function to determine the day of the week a person was born given his or

ID: 3676167 • Letter: W

Question

Write a function to determine the day of the week a person was born given his or her birthday. The following steps should be used to find the day of the week corresponding to any date from 1901 through the present.

In the following explanation, the following terms will be helpful. Assuming I type in my birthday as "6 10 1981":

The month is 6.

The day of the month is 10.

The year is 1981.

Find the number of years since 1900, and put it into a variable called yy. Simply subtract 1900 from the year to get this.

Divide the number of years since 1900 by 4. Put the quotient in a variable called total. For example, if the person was born in 1983, divide 83 by 4 and store 20 in total.

Also add the number of years since 1900 to total.

Add the day of the month to total.

Using the function month_offset() described in the following, find the "month offset" and add it to total.

If the year is a leap year and if the month you are working with is either January or February, then subtract 1 from the total. You can use the function is_leap() provided to determine if the year is a leap year.

Find the remainder when total is divided by 7. Pass this remainder to the function weekday_name() you wrote to determine the day of the week the person was born.

Finally, build up a single String value containing the whole date (day of week, month, day, year). You'll need to use the function month_name() you wrote to show the month name rather than its number.

Return that String value.

Whew. Here's some code to get you started.

Welcome to MCIS5103's fantastic birth-o-meter!

All you have to do is enter your birthday, and it will tell you the day of the week on which you were born.

Some automatic tests....

12 10 2003 => Wednesday, December 10, 2003

2 13 1976 => Friday, February 13, 1976

2 13 1977 => Sunday, February 13, 1977

7 2 1974 => Tuesday, July 2, 1974

1 15 2003 => Wednesday, January 15, 2003

10 13 2000 => Friday, October 13, 2000

Now it's your turn! What's your birthday?

Birthday (mm dd yyyy): 11 11 2010

You were born on Thursday, November 11, 2010!

The function that gives you the "month offset" should be following the table below

Month

Month offset

1

1

2

4

3

4

4

0

5

2

6

5

7

0

8

3

9

6

10

1

11

4

12

6

anything else

-1

Month

Month offset

1

1

2

4

3

4

4

0

5

2

6

5

7

0

8

3

9

6

10

1

11

4

12

6

anything else

-1

Explanation / Answer

import java.util.Scanner;

public class WeekdayCalculator {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Welcome to Mr. Mitchell's fantastic birth-o-meter!");
        System.out.println();
        System.out.println("All you have to do is enter your birth date, and it will");
        System.out.println("tell you the day of the week on which you were born.");
        System.out.println();
        System.out.println("Some automatic tests....");
        System.out.println("12 10 2003 => " + weekday(12, 10, 2003));
        System.out.println(" 2 13 1976 => " + weekday(2, 13, 1976));
        System.out.println(" 2 13 1977 => " + weekday(2, 13, 1977));
        System.out.println(" 7 2 1974 => " + weekday(7, 2, 1974));
        System.out.println(" 1 15 2003 => " + weekday(1, 15, 2003));
        System.out.println("10 13 2000 => " + weekday(10, 13, 2000));
        System.out.println();

        System.out.println("Now it's your turn! What's your birthday?");
        System.out.print("Birth date (mm dd yyyy): ");
        int mm = keyboard.nextInt();
        int dd = keyboard.nextInt();
        int yyyy = keyboard.nextInt();

        System.out.println("You were born on " + weekday(mm, dd, yyyy));
    }

    public static String weekday(int mm, int dd, int yyyy) {
        int yy, total;
        String date;

        // bunch of calculations go here
        yy = yyyy - 1900;

        if (is_leap(yyyy) && (mm == 1 || mm == 2)) {
            total = yy / 4 + yy + dd + month_offset(mm) - 1;
        } else {
            total = yy / 4 + yy + dd + month_offset(mm);
        }
      
        date = weekday_name(total % 7) + ", " + month_name(mm) + " " + dd + ", " + yyyy;

        return date;
    }
// paste your functions from MonthName, WeekdayName, and MonthOffset here

    public static String month_name(int num) {
        String month = "";
        switch (num) {
            case 1:
                month = "January";
                break;
            case 2:
                month = "February";
                break;
            case 3:
                month = "March";
                break;
            case 4:
                month = "April";
                break;
            case 5:
                month = "May";
                break;
            case 6:
                month = "June";
                break;
            case 7:
                month = "July";
                break;
            case 8:
                month = "August";
                break;
            case 9:
                month = "September";
                break;
            case 10:
                month = "October";
                break;
            case 11:
                month = "November";
                break;
            case 12:
                month = "December";
                break;
            default:
                month = "error";
                break;
        }
        return month;
    }

    public static String weekday_name(int weekday) {
        String result;
        switch (weekday) {
            case 1:
                result = "Sunday";
                break;
            case 2:
                result = "Monday";
                break;
            case 3:
                result = "Tuesday";
                break;
            case 4:
                result = "Wednesday";
                break;
            case 5:
                result = "Thursday";
                break;
            case 6:
                result = "Friday";
                break;
            case 7:
                result = "Saturday";
                break;
            case 0:
                result = "Saturday";
                break;
            default:
                result = "Error";
                break;
        }

        return result;
    }

    public static int month_offset(int num) {
        int daysOffset;
        switch (num) {
            case 1:
                daysOffset = 1;
                break;
            case 2:
                daysOffset = 4;
                break;
            case 3:
                daysOffset = 4;
                break;
            case 4:
                daysOffset = 0;
                break;
            case 5:
                daysOffset = 2;
                break;
            case 6:
                daysOffset = 5;
                break;
            case 7:
                daysOffset = 0;
                break;
            case 8:
                daysOffset = 3;
                break;
            case 9:
                daysOffset = 6;
                break;
            case 10:
                daysOffset = 1;
                break;
            case 11:
                daysOffset = 4;
                break;
            case 12:
                daysOffset = 6;
                break;
            default:
                daysOffset = -1;
                break;
        }
        return daysOffset;
    }

    public static boolean is_leap(int year) {
        // years which are evenly divisible by 4 are leap years,
        // but years divisible by 100 are not leap years,
        // though years divisible by 400 are leap years
        boolean result;

        if (year % 400 == 0 || year % 4 == 0) {
            result = true;
        } else if (year % 100 == 0) {
            result = false;
        } else {
            result = false;
        }

        return result;
    }
}


sample output

                                                                                                                                                            
All you have to do is enter your birth date, and it will                                                                                                    
tell you the day of the week on which you were born.                                                                                                        
                                                                                                                                                            
Some automatic tests....                                                                                                                                    
12 10 2003 => Wednesday, December 10, 2003                                                                                                                  
2 13 1976 => Friday, February 13, 1976                                                                                                                     
2 13 1977 => Sunday, February 13, 1977                                                                                                                     
7 2 1974 => Tuesday, July 2, 1974                                                                                                                         
1 15 2003 => Wednesday, January 15, 2003                                                                                                                   
10 13 2000 => Friday, October 13, 2000                                                                                                                      
                                                                                                                                                            
Now it's your turn! What's your birthday?                                                                                                                  
Birth date (mm dd yyyy): 11 11 2010                                                                                                                         
You were born on Thursday, November 11, 2010                                                                                                                

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote