Question

In: Computer Science

What day of the week is a given date? i.e. On what day of the week...

What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday

Write a function is leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not.

.Then, write a function days_since() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and returns the total number of days passed since the 1st January 1900. (For example, days_since(2, 1, 1900) should return 1.

Finally, write a function week_day() which takes 3 input integers day, month, year, representing a date (after 1st January 1900), and prints the day of the week corresponding to the date

Dont use any inbuilt function, use looping.

Solutions

Expert Solution

Function to check Leap year

static boolean isLeap(int year)
        {
                if (year % 400 == 0)
                        return true;
                if (year % 100 == 0)
                        return false;
                if (year % 4 == 0)
                        return true;
                return false;
        }

Function to return the total number of days passed since the 1st January 1900

static int monthDays[] = {31, 28, 31, 30, 31, 30, 
                                                        31, 31, 30, 31, 30, 31}; 
        static int countLeapYears(int m,int y) 
        { 
                int years = y; 
                if (m<= 2) 
                { 
                        years--; 
                } 
                return years / 4 - years / 100 + years / 400; 
        } 
        static int days_since(int d,int m,int y) 
        { 
                int n1 = 1900 * 365 + 1; 
                for (int i = 0; i < 1 - 1; i++) 
                { 
                        n1 += monthDays[i]; 
                } 
                n1 += countLeapYears(1,1900); 
                int n2 = y * 365 + d; 
                for (int i = 0; i < m - 1; i++) 
                { 
                        n2 += monthDays[i]; 
                } 
                n2 += countLeapYears(m,y); 
                return (n2 - n1); 
        } 

Function to prints the day of the week corresponding to the date

static String week_day(int d, int m, int y) 
{ 
    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; 
    String d1[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    y -= (m < 3) ? 1 : 0; 
    return d1[( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7]; 
} 

All the above funcrions are putted in switch case to test

Source Code

import java.util.*;
public class Date
{
    static boolean isLeap(int year)
    {
        if (year % 400 == 0)
            return true;
        if (year % 100 == 0)
            return false;
        if (year % 4 == 0)
            return true;
        return false;
    }
    static int monthDays[] = {31, 28, 31, 30, 31, 30, 
                            31, 31, 30, 31, 30, 31}; 
    static int countLeapYears(int m,int y) 
    { 
        int years = y; 
        if (m<= 2) 
        { 
            years--; 
        } 
        return years / 4 - years / 100 + years / 400; 
    } 
    static int days_since(int d,int m,int y) 
    { 
        int n1 = 1900 * 365 + 1; 
        for (int i = 0; i < 1 - 1; i++) 
        { 
            n1 += monthDays[i]; 
        } 
        n1 += countLeapYears(1,1900); 
        int n2 = y * 365 + d; 
        for (int i = 0; i < m - 1; i++) 
        { 
            n2 += monthDays[i]; 
        } 
        n2 += countLeapYears(m,y); 
        return (n2 - n1); 
    } 
    static String week_day(int d, int m, int y) 
    { 
        int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; 
        String d1[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        y -= (m < 3) ? 1 : 0; 
        return d1[( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7]; 
    } 
    public static void main() 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 1 to check Leap Year\nEnter 2 to find the total number of days passed since the 1st January 1900\nEnter 3 to to prints the day of the week corresponding to the date");
        System.out.println("Enter your choice");
        int ch=sc.nextInt();
        switch(ch)
        {
            case 1: System.out.println("Enter year to check");
                    int year=sc.nextInt();
                    System.out.println(isLeap(year));break;
            case 2: System.out.println("Enter day to check");
                    int d=sc.nextInt();
                    System.out.println("Enter month to check");
                    int m=sc.nextInt();
                    System.out.println("Enter year to check");
                    int y=sc.nextInt();
                    System.out.println("Number of day since 1st january 1990 ="+days_since(d,m,y));break;
            case 3: System.out.println("Enter day to check");
                    int d1=sc.nextInt();
                    System.out.println("Enter month to check");
                    int m1=sc.nextInt();
                    System.out.println("Enter year to check");
                    int y1=sc.nextInt();
                    System.out.println("Day of the week is "+week_day(d1,m1,y1));break;
            default:System.out.println("Wrong Input");break;
           
        }
    }
}

Output of all Functions used above

Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
1
Enter year to check
2020
true
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
2
Enter day to check
2
Enter month to check
1
Enter year to check
1990
Number of day since 1st january 1990 =32873
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice

2
Enter day to check
2
Enter month to check
1
Enter year to check
1900
Number of day since 1st january 1990 =1
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
3
Enter day to check
13
Enter month to check
10
Enter year to check
2020
Day of the week isTuesday
Enter 1 to check Leap Year
Enter 2 to find the total number of days passed since the 1st January 1900
Enter 3 to to prints the day of the week corresponding to the date
Enter your choice
3
Enter day to check
13
Enter month to check
10
Enter year to check
2020
Day of the week is Tuesday

Related Solutions

USE PYTHON to find out What day of the week is a given date? i.e. On...
USE PYTHON to find out What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is_leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing...
Write a query to return the date and day of the week of the first day...
Write a query to return the date and day of the week of the first day of the month two years from today. If today is 10/16/20, then the expect output is 10/01/2022 and the day of the week is thursday. I am using postgresql.
Day of week Write a program that asks the user for a date (year, then month,...
Day of week Write a program that asks the user for a date (year, then month, then day, each entered as a number on a separate line), then calculates the day of the week that date falls on (according to the Gregorian calendar) and displays the day name. You may not use any date functions built into Python or its standard library, or any other functions that do this work for you. Hint: Reverend Zeller may be useful, but his...
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
Use Zeller’s algorithm to figure out the days of the week for a given date in...
Use Zeller’s algorithm to figure out the days of the week for a given date in c. W = (d + 13(m+1)/5 + y + y/4 + c/4 - 2c + 6) mod 7 Y is the year minus 1 for January or February, and the year for any other month y is the last 2 digits of Y c is the first 2 digits of Y d is the day of the month (1 to 31) m is the...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
***This is the only information I am given. **** Week 2 Date Transaction description 9 Purchased...
***This is the only information I am given. **** Week 2 Date Transaction description 9 Purchased 14 Specialist Tennis Raquets from Addax Sports for $232 each, terms net 30. 10 Mick's Sporting Goods paid the full amount owing on their account. Since Mick's Sporting Goods has been a loyal customer from the day the business commenced, a 10% discount was given for this early repayment. 11 Paid the full amount owing to Sports 'R Us, Check No. 873. Payment fell...
Determining Maturity Date Find the maturity date of the following: 120-day note dated May 16 90-day...
Determining Maturity Date Find the maturity date of the following: 120-day note dated May 16 90-day note dated November 9 Calculate Maturity Value Find the maturity value of the following: $8,800 6% 9 months $12,000 2% 75 days Journalizing Notes for Buyer and Seller For each of the following transactions for Jackson Co. (the seller), journalize what the entry would be for the buyer (North Co.). Jackson Company uses the periodic method. Accounts Receivable, North Co. 7,800    Sales 7,800 Sold...
The Statement of Financial Position date (i.e., the year-end date) for Company AB is 31 December...
The Statement of Financial Position date (i.e., the year-end date) for Company AB is 31 December 2018. The financial accounts for Company AB were approved on 1 March 2019. On 1 February 2019, a major customer of Company AB, announced that they were bankrupt and could not pay the huge amounts they owed to Firm AB For Company AB for the financial year-ended 31 December 2018 this event: Select one: a. None of these answers b. Is not an adjusting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT