Question

In: Computer Science

write a program that computes and displays the number of days that have elapsed between January...

write a program that computes and displays the number of days that have elapsed between January 1, 1900 and today (the day the program is run).
The program must be designed as follows:
Within a class named Elapsed_Days, provide the following functions:
• bool is_year_valid(long year)
returns true if year is >= 1900; otherwise false
• bool is_month_valid(long month)
returns true if month is between 1 and 12 (inclusive); otherwise false
• bool is_leap_year(long year) returns true if year is a leap year
• long return_days_in_month(long year, long month) returns the number of days for the month month in year year
• long return days_in_year(year) returns the number of days in year
• void set_elapsed_days(long elapsed_days) sets the field value set_elapsed_days
• long get_elapsed_days()
returns the field value set_elapsed_days
• void compute_elapsed days()
returns the number of days elapsed between January 1, 1900 and the day the program is run
• void display_elapsed_days()
displays the number of days elapsed in the form “The number of days elapsed since Jan 1, 1900 is xxxx.” Where xxxx is the number of the elapsed days
• A constructor that determines the number of elapsed days
• A suitable destructor
Within a main(...) driver function:
• Instantiates an instance of elapsed_days
• Displays the number of elapsed days

c++

Solutions

Expert Solution

#include<iostream>
using namespace std;

class Elapsed_Days
{
        // members to store the current date, month, year and total days elapsed since 1900
        private:
                long curr_year, curr_month, curr_day, total_elapsed_day;
                
        
        public:
                // constructor, checking whether the data entered is correct or not
                Elapsed_Days(long year, long month, long day)
                {
                        // if the data is correct then setting that data.
                        if(is_year_valid(year) && is_month_valid(month))
                        {
                                curr_year = year;
                                curr_month = month;
                                curr_day = day;
                        }
                        // if the date is not valid
                        else
                        {
                                cout<<"Date is not valid"<<endl;
                                exit(1);
                        }
                        
                }
                
                // destructor
                ~Elapsed_Days()
                {
                        cout<<"In deconstructor";
                }
                
                // checking if the year passed is valid or not
                bool is_year_valid(long year)
                {
                        if(year >= 1900)
                                return true;
                        
                        return false;
                }
                
                // checking if the month passed is valid or not
                bool is_month_valid(long month)
                {
                        if(month <= 12 && month >=1)
                                return true;
                                
                                return false;
                }
                
                // checking if the given year is a leap year or not
                bool is_leap_year(long year)
                {
                        if(year%100 == 0)
                        {
                                if(year%400 == 0)
                                        return true;
                                        
                                return false;
                        }
                        else
                        {
                                if(year%4 == 0)
                                        return true;
                                        
                                return false;
                        }
                }
                
                // calculating the number of days in a month
                long return_days_in_month(long year, long month)
                {
                        bool is_leap = is_leap_year(year);
                        
                        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                                return 31;
                        }
                        else if(month == 4 || month == 6 || month == 9 || month == 11)
                        {
                                return 30;
                        }
                        else
                        {
                                if(is_leap)
                                        return 29;
                                        
                                return 28;
                        }       
                }
                
                //calculating the number of days in a given year
                long return_days_in_year(long year)
                {
                        if(year == curr_year)
                        {
                                long days = 0;
                                for(int i=1; i<curr_month; i++)
                                {
                                        days += return_days_in_month(year,i);
                                }
                                // counting till last day
                                days += curr_day-1;
                                return days;
                        }
                        else
                        {
                                if(is_leap_year(year))
                                {
                                        return 366;
                                }
                                return 365;
                        }
                }
                
                // setting the field value
                void set_elapsed_days(long elapsed_days)
                {
                        total_elapsed_day = elapsed_days;
                }
                
                // computing the total number of days elapsed
                void compute_elapsed_days()
                {
                        long days = 0;
                        for(int i=1900 ; i<=curr_year; i++)
                        {
                                days += return_days_in_year(i);
                        }
                        set_elapsed_days(days);
                }
                
                // showing the number of days elpased
            void display_elapsed_days()
            {
                compute_elapsed_days();
                cout<<"The number of days elapsed since Jan 1, 1900 is "<<total_elapsed_day<<endl;
                }
                
                
                
                long get_elapsed_days()
                {
                        return total_elapsed_day;
                }
                        
};

int main()
{
        long day, month, year;
        
        cout<<"Enter the day:"<<endl;
        cin>>day;
        
        cout<<"Enter the month:"<<endl;
        cin>>month;
        
        cout<<"Enter the year:"<<endl;
        cin>>year;
        
        Elapsed_Days d1(year, month,day);
        d1.display_elapsed_days();
}


Related Solutions

C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital...
C++ Overloaded Hospital Write a program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an in-patient or an out-patient. Please keep asking the user until the user enters the valid choice. Please refer to the test cases for more details. If the patient was an in-patient the following data should be entered: • The number of days spent in the hospital • The daily rate •...
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT