Question
For all problems you must: -Write Python code -Test, debug, and execute your code -Submit a copy of your commented source code 1. a) Complete
For all problems you must:
-Write Python code
-Test, debug, and execute your code
-Submit a copy of your commented source code
1.
a) Complete the partial implementation of the Date class by implementing the remaining methods:
monthName(): Returns the Gregorian month name of this date.
isLeapYear(): Determines if this date falls in a leap year and returns the appropriate boolean value.
numDays(): Returns the number of days as a positive integer between this date and the otherDate.
advanceBy(): Advances the date by the given number of days. The date is incremented if days is positive and decremented if days is negative. The date is capped to November 24, 4714 BC, if necessary.
b) Add additional operations to the Date class:
--dayOfWeekName(): returns a string containing the name of the day.
--isWeekDay(): determines if the date is a weekday.
--isSolstice(): determines if the date is the summer or winter solstice.
# date.py
# Implements a proleptic Gregorian calendar date as a Julian day number.
class Date:
# Asert [ assert EXPRESSION [',' EXPRESSION ] ] will raise an error
# if the first expression is wrong. If another expression is given
# it will executed that expression in raising the error.
def __init__(self,month,day,year):
self._julianDay = 0
assert self.isValidGregorian(month,day,year),"Invalid Gregorian Date."
tmp = 0
if month < 3:
tmp = -1
self._julianDay = day - 32075 + \
(1461 * (year + 4800 + tmp) // 4 ) + \
(367 * (month - 2 - tmp *12) // 12) - \
(3 * ((year + 4900 + tmp) // 100) // 4)
def isValidGregorian(self,month,day,year):
return_code = True
if month == None or day == None or year == None:
return_code = False
if month > 12 or day > 31:
return_code = False
if month < 0 or day < 0:
return_code = False
return return_code
def month(self):
return (self._toGregorian())[0]
def day (self):
return (self._toGregorian())[1]
def year (self):
return (self._toGregorian())[2]
def dayOfWeek(self):
month,day,year = self._toGregorian()
if month < 3:
month = month +12
year = year -1
return ((13 * month + 3) // 5 + day + year + year //4 - year // 100 + year // 400) % 7
def __str__(self):
month, day, year = self._toGregorian()
return "%02d/%02d/%04d" % (month,day,year)
def __eq__ (self,otherDate):
return self._julianDay == otherDate._julianDay
def __lt__(self, otherDate):
return self._julianDay < otherDate._julianDay
def __le__(self,otherDate):
return self._julianDay <= otherDate._julianDay
def _toGregorian(self):
A = self._julianDay + 68569
B = 4 * A // 146097
A = A - (146097 * B + 3) //4
year = 4000 * (A+1) // 1461001
A = A - (1461 * year // 4) + 31
month = 80*A // 2447
day = A - (2447 * month //80)
A = month //11
month = month + 2 - (12*A)
year = 100 * (B-49) + year + A
return month,day,year
# main_date.py
# Extracts a collection of birth dates from the user and determines
# if each individual is at least 21 years of age.
from date import Date
def main():
# Date before which a person must have been born to be 21 or older.
bornBefore = Date(9, 9, 1994)
# Extract birth dates from the user and determine if 21 or older.
date = promptAndExtractDate()
while date is not None :
if date <= bornBefore :
print( "Is at least 21 years of age: ", date )
else :
print( "You are too young!")
date = promptAndExtractDate()
# Prompts for and extracts the Gregorian date components. Returns a
# Date object or None when the user has finished entering dates.
def promptAndExtractDate():
print( "Enter a birth date." )
month = int( input("month (0 to quit): ") )
if month == 0 :
return None
else :
day = int( input("day: ") )
year = int( input("year: ") )
return Date( month, day, year )
# Call the main routine.
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started