Changes

Jump to: navigation, search

OPS445 Online Lab7

39 bytes added, 08:35, 13 March 2022
m
Part 1 - Classes and Methods for our Time objects
# Student ID: [seneca_id]
class Time:
"""Simple object type for time of the day. data attributes: hour, minute, second function attributes: __init__, __str__, __repr__ time_to_sec, format_time, change_time, sum_time """ def __init__(self,hour=12,minute=0,second=0): """constructor for time object""" self.hour = hour self.minute = minute self.second = second def format_time(self): """Return time object (t) as a formatted string""" return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second) def sum_times(self, t2): """Add two time objests and return the sum.""" """Change this to become object method for our Time object (refer to lab7c.py and change_time() method below)""" return sum
def sum_timeschange_time(self, t2seconds): """Add two time objests and return the sum time_seconds = self."""time_to_sec() """Change this to become object method for our Time object nt = sec_to_time(refer to lab7ctime_seconds + seconds) self.hour, self.py and change_time() method below)"""minute, self.second = nt.hour, nt.minute, nt.second return sumNone
def change_timetime_to_sec(self, seconds): time_seconds = self.time_to_sec() '''convert a time object to a single integer representing the nt = sec_to_time(time_seconds + number of seconds)from mid-night''' minutes = self.hour, * 60 + self.minute, seconds = minutes * 60 + self.second = nt.hour, nt.minute, nt.second return Noneseconds
def time_to_sec(self): '''convert a time object to a single integer representing the number of seconds from mid-night''' minutes = self.hour * 60 + self.minute seconds = minutes * 60 + self.second return seconds  def valid_time(self): """check for the validity of the time object attributes: 24 > hour > 0, 60 > minute > 0, 60 > second > 0 """ if self.hour < 0 or self.minute < 0 or self.second < 0: return False if self.minute >= 60 or self.second >= 60 or self.hour >= 24: return False return True
def sec_to_time(seconds):
572
edits

Navigation menu