Create a Date class. Use polymorphism to increment the date by 1 day and print in different formats.
Question
Create a Date class. Use polymorphism to increment the date by 1 day and print in different formats.
Solution
Sure, here is a simple implementation in Python:
class Date:
def __init__(self, day, month, year):
self.day = day
self.month = month
self.year = year
def increment(self):
self.day += 1
if self.day > 30:
self.day = 1
self.month += 1
if self.month > 12:
self.month = 1
self.year += 1
def print_date(self):
print(f"{self.day}/{self.month}/{self.year}")
def print_date_us(self):
print(f"{self.month}/{self.day}/{self.year}")
date = Date(31, 12, 2020)
date.increment()
date.print_date() # prints: 1/1/2021
date.print_date_us() # prints: 1/1/2021
This is a very basic implementation and does not take into account different month lengths or leap years. For a more robust solution, you might want to use a date library.
Similar Questions
Write a program to print corresponding day based on given inputInput Format:Accept integer as a inputOutput Format:Print corresponding day(0-6) or "Invalid" day- Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,SaturdayConstraints:1 <= day_num <= 10^15
Create a class called Date that includes three pieces of information as instance variables—a month, a day and a year. Your class should have a constructor that initializes the three instance variables after validating the given date. Provide a getDetails() member method to read the date. Provide a method displayDate() that displays the month, day and year separated by forward slashes(/). Write a C++ application and pseudocode that demonstrates classDate’s capabilities.Input : Read Month, Day and Year (Line by line)Output :Print Date that displays the month, day and year separated by forward slashes(/)Note: print "Invalid Date" other than the following2000 < Year < 20250 < Day <= 31 ( for the months Jan, March, May, July, August, October, December)0 < Day <= 30 ( for the months April, June, September, and November)0 < Day <= 28 ( for the month February ) and (0 < Day <= 29), if month = 2 and year = leap year0 < Month < = 12
Write a C++ program to overload the + operator to increment the date object.Sample input23 //year10//month8//daySample output24 11 9
create a class called date, with integer data members for day, month, and year. the comprises of member functions: 1. to display date in DD/MM/YYYY format 2. to subtract an integer from date object 3. to subtract one date from another using oops in c++
Create a class named Year that contains a data field that holds the number of days in theyear. Include a get method that displays the number of days and a constructor that setsthe number of days to 365. Create a subclass named LeapYear. LeapYear’s constructoroverrides Year’s constructor and sets the day field to 366.Write a program that instan6ates one object of each class and displays their data.Add a method named daysElapsed() to the Year class you created in Exercise 5a. ThedaysElapsed() method accepts two arguments represen6ng a month and a day; it returnsan integer indica6ng the number of days that have elapsed since January 1 of the year.Create a daysElapsed() method for the LeapYear class that overrides the method in theYear class. Write a program that calculates the days elapsed on March 1 for a Year and fora LeapYear.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.