Answered step by step
Verified Expert Solution
Link Copied!

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 10.4.2'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 _total_seconds attribute. Modify the bodies of the hour, minute and second properties' methods to get and set_total_seconds. Re-execute Section 10.4'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 set_time to reset the time altogether. I have re-written 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 read-write properties."""
def __init__(self, total_seconds=0):
"""Initialize total_seconds attribute."""
self._total_seconds = total_seconds
@property
def hour(self):
"""Return the hour in seconds."""
return self._total_seconds //3600*3600
@hour.setter
def hour(self, hour):
"""Set the hour in seconds.
if not (0<= hour <24):
raise ValueError(f'Hour ({hour}) must be 0-23')"""
self._hour = self._total_seconds //3600*3600
self._hour - hour
@property
def minute(self):
"""Return the minute in seconds."""
return self._total_seconds %3600//60*60
@minute.setter
def minute(self, minute):
"""Set the minute in seconds.
if not (0<= minute <60):
raise ValueError(f'Minute ({minute}) must be 0-59')"""
self._minute = self._total_seconds %3600//60*60
self.minute = minute
@property
def second(self):
"""Return the second."""
return self._total_seconds %60
@second.setter
def second(self, second):
"""Set the second.
if not (0<= second <60):
raise ValueError(f'Second ({second}) must be 0-59')"""
self.second -= self._total_seconds %60
self.second += second
def set_time(self, total_seconds=0):
"""Set value of total seconds."""
self.total_seconds = total_seconds
def __repr__(self):
"""Return Time string for repr()."""
return f'Time(hour={self.hour} seconds, minute={self.minute} seconds, second={self.second} seconds)'
def __str__(self):
"""Return Time string in seconds since midnight."""
s_midnight = self.hour + self.minute + self.second
return f'Seconds since midnight: {s_midnight}'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions