In: Computer Science
There are (at least) two ways in which you can make a 12-hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can simply leave the clock to work internally as a 24-hour clock but change the display string of the clock display to show4:23 or 4:23pm when the internal value is 16:23. Implement both versions. Which option is easier? Which is better? Why?
use python
Here is the completed code for this problem for both implementations. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#a class representing a 12 hour clock
class Clock12Hour:
#constructor taking hours, minutes and am/pm
string
def __init__(self, hrs,
mins, am_pm):
#initializing values
using setter methods
self.setHours(hrs)
self.setMinutes(mins)
self.setAMorPM(am_pm)
#setter/mutator methods
def setHours(self,
hrs):
#validating hrs
before assigning to hours
assert hrs >= 1 and hrs <=
12
self.hours=hrs
def setMinutes(self,
mins):
assert
mins >= 0 and mins < 60
self.minutes=mins
def
setAMorPM(self,am_pm):
am_pm =
am_pm.upper()
assert
am_pm == 'AM' or am_pm ==
'PM'
self.am_pm =
am_pm
# getter/accessor methods
def getHours(self):
return
self.hours
def getMinutes(self):
return
self.minutes
def getAMorPM(self):
return
self.am_pm
#returns a string in format hh:mm
AM/PM
def __str__(self):
return
'{:02d}:{:02d}
{}'.format(self.hours,self.minutes,self.am_pm)
#a class representing a 24 hour clock
class Clock24Hour:
#constructor taking hours and minutes
def __init__(self, hrs,
mins):
#initializing values
using setter methods
self.setHours(hrs)
self.setMinutes(mins)
def setHours(self, hrs):
#ensuring that hrs
is between 0 and 23
assert hrs >= 0 and hrs <
24
self.hours = hrs
def setMinutes(self,
mins):
assert
mins >= 0 and mins < 60
self.minutes =
mins
def getHours(self):
return
self.hours
def getMinutes(self):
return
self.minutes
#returns whether current time is AM or
PM
def getAMorPM(self):
if
self.hours>=12:
#afternoon
return
'PM'
else:
#morning
return 'AM'
#returns a string in hh:mm AM/PM
format
def __str__(self):
#getting hours
hours=self.hours
#initially assuming
time is AM
am_pm='AM'
#if hours
> 12, subtracting 12 from hours to convert to 12 hour
system
if
hours>12:
hours=hours-12
am_pm='PM'
#also, if
hours is 12, it is PM
if
hours==12:
am_pm='PM'
#returning
time in hh:mm AM/PM format
return '{:02d}:{:02d}
{}'.format(hours, self.minutes, am_pm)
#creating objects of the two implementations and dispalaying
time
c1=Clock12Hour(4,23,'pm')
c2=Clock24Hour(16,23)
#both c1 and c2 will display the same time
print(c1)
print(c2)
#storing time in 24 hour format is preferred, because it makes
everything easy
#when we need to manipulate time, also it is not difficult to
convert 24 hr clock
#into 12 hour system. one more benefit is that it's easier to sort
time in 24 hr
#format than time in 12 hr format
#output
04:23 PM
04:23 PM