class Time: """ Class Time with read-write properties. """ def __init__(self, hour=0, minute=0, second=0): """ Constructor of a Time object with default values. """ self.h = hour # 0-23 self.minute = minute # 0-59 self.second = second # 0-59 @property def h(self): """ @return the hour in the 12-hour format. """ return self._hour if self._hour <= 12 else self._hour - 12 @h.setter def h(self, hour): """ Set the hour. @param hour the hour to set. """ if not (0 <= hour < 24): raise ValueError(f'Hour ({hour}) must be 0-23') self._hour = hour @property def minute(self): """ @return the minute. """ return self._minute @minute.setter def minute(self, minute): """ Set the minute. @param minute the minute to set. """ if not (0 <= minute < 60): raise ValueError(f'Minute ({minute}) must be 0-59') self._minute = minute @property def second(self): """ @return the second. """ return self._second @second.setter def second(self, second): """ Set the second. @param second the second to set. """ if not (0 <= second < 60): raise ValueError(f'Second ({second}) must be 0-59') self._second = second def set_time(self, hour=0, minute=0, second=0): """ Set the hour, minute, and second by invoking the setters. @param hour the hour to set. @param minute the minute to set. @param second the second to set. """ self.hour = hour self.minute = minute self.second = second def __repr__(self): """ @return the string representation of the Time object. """ return (f'Time(hour={self._hour}, minute={self._minute}, ' + f'second={self._second})') def __str__(self): """ @return a Time string in 12-hour clock format for calls to str() or to print(). """ return (('12' if self._hour in (0, 12) else str(self._hour%12)) + f':{self._minute:0>2}:{self._second:0>2}' + (' AM' if self._hour < 12 else ' PM')) ########################################################################## # (C) Copyright 2019 by Deitel & Associates, Inc. and # # Pearson Education, Inc. All Rights Reserved. # # # # DISCLAIMER: The authors and publisher of this book have used their # # best efforts in preparing the book. These efforts include the # # development, research, and testing of the theories and programs # # to determine their effectiveness. The authors and publisher make # # no warranty of any kind, expressed or implied, with regard to these # # programs or to the documentation contained in these books. The authors # # and publisher shall not be liable in any event for incidental or # # consequential damages in connection with, or arising out of, the # # furnishing, performance, or use of these programs. # ########################################################################## # Additional material (C) Copyright 2024 by Ronald Mak