Answered step by step
Verified Expert Solution
Question
1 Approved Answer
( Modifying the Internal Data Representation of a Class ) Section 1 0 . 4 . 2 ' s Time class represents the time as
Modifying the Internal Data Representation of a Class Section s Time class represents the time as three integer values. Modify the class to store the time as the total number of seconds since midnight. Replace the hour, minute and second attributes with one totalseconds attribute. Modify the bodies of the hour, minute and second properties' methods to get and settotalseconds. Reexecute Section s IPython session using the modified Time class to show that the updated class Time is interchangeable with the original one. This problem has been asked before but none of the solutions that were given do what the assignment is asking: represent the hour, minute, and second in seconds. Also, be able to call the methods to alter each property or call settime to reset the time altogether. I have rewritten the script to get closer to the results being asked for but my methods are not working. Please help! Here is the code I have: class Time:
Class Time with readwrite properties."""
def initself totalseconds:
Initialize totalseconds attribute."""
self.totalseconds totalseconds
@property
def hourself:
Return the hour in seconds."""
return self.totalseconds
@hour.setter
def hourself hour:
Set the hour in seconds.
if not hour :
raise ValueErrorfHour hour must be
self.hour self.totalseconds
self.hour hour
@property
def minuteself:
Return the minute in seconds."""
return self.totalseconds
@minute.setter
def minuteself minute:
Set the minute in seconds.
if not minute :
raise ValueErrorfMinute minute must be
self.minute self.totalseconds
self.minute minute
@property
def secondself:
Return the second."""
return self.totalseconds
@second.setter
def secondself second:
Set the second.
if not second :
raise ValueErrorfSecond second must be
self.second self.totalseconds
self.second second
def settimeself totalseconds:
Set value of total seconds."""
self.totalseconds totalseconds
def reprself:
Return Time string for repr
return f'Timehourselfhour seconds, minuteselfminute seconds, secondselfsecond seconds
def strself:
Return Time string in seconds since midnight."""
smidnight self.hour self.minute self.second
return f'Seconds since midnight: smidnight
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