You are on page 1of 4

CSC108H Fall 2022 Worksheet: Object-Oriented Programming – Class Event

For each method, if the docstring example is incomplete, complete it, and then implement the method body.

class Event:
"""A new calendar event."""

def __init__(self, start_time: int, end_time: int, event_name: str) -> None:
"""Initialize a new event that starts at start_time, ends at end_time,
and is named event_name.

Precondition: 0 <= start_time < end_time <= 23

>>> e = Event(12, 13, 'Lunch')


>>> e.start_time
12
>>> e.end_time
13
>>> e.name
'Lunch'
"""
self.start_time = start_time
self.end_time = end_time
self.name = event_name

def rename(self, new_name: str) -> None:


"""Change the name of this event to new_name.

>>> e = Event(12, 13, 'Lunch')


>>> e.rename(‘Dinner’)
>>> e
Event(12, 13, ‘Dinner’)
"""
self.name = new_name

def duration(self) -> int:


"""Return the duration of this event.

>>> e = Event(9, 10, 'Lecture')


>>> e.duration()
1
"""
return self.end_time - self.start_time
CSC108H Fall 2022 Worksheet: Object-Oriented Programming – Class Event

Use str.format method call so that code


def __str__(self) -> str:
looks neat → use placeholders, which are
"""Return a string representation of this event.
locations in string that we want to replace
>>> e = Event(6, 7, 'Run')
with actual values
>>> str(e)
'Run: from 6 to 7'
"""
return ‘{0}: from {1} to {2}’.format(self.name, self.start_time, self.end_time)’

def __eq__(self, other: 'Event') -> bool:


"""Return True if and only if this event has the same start time,
end time, and name as other.

>>> e1 = Event(6, 7, 'Run')


>>> e2 = Event(6, 7, ‘Run’)
>>> e1 == e2
True
"""
return self.start_time == other.start_time and self.end_time == other.end_time and \
self.name == other.name

def overlaps(self, other: 'Event') -> bool:


"""Return True if and only if this event overlaps with event other.

>>> e1 = Event(6, 7, 'Run')


>>> e2 = Event(0, 7, 'Sleep')
>>> e1.overlaps(e2)
True
"""
return self.start_time <= other.start_time < self.end_time or \
other.start_time <= self.start_time < other.end_time

Check whether the second start time is between the first


start and end time OR whether the first start time is
between the second start and end time
CSC108H Fall 2022 Worksheet: Object-Oriented Programming – Class Day

For each method, implement the body.

import event

class Day:
"""A calendar day and its events."""

def __init__(self, day: int, month: str, year: int) -> None:
"""Initialize a day on the calendar with day, month and year,
and no events.

>>> d = Day(7, 'December', 2022)


>>> d.day
7
>>> d.month
'December'
>>> d.year
2022
>>> d.events
[]
"""
self.day = day
self.month = month
self.year = year
self.events = []

def schedule_event(self, new_event: 'Event') -> None:


"""Schedule new_event on this day, even if it overlaps with
an existing event. Later we will improve this method.

>>> d = Day(7, 'December', 2022)


>>> e = event.Event(11, 12, 'Meeting')
>>> d.schedule_event(e)
>>> d.events[0] == e
True
"""
self.events.append(new_event)
CSC108H Fall 2022 Worksheet: Object-Oriented Programming – Class Day
Using functions/methods from imported
modules → for ex. using Event() from event.py
def __str__(self) -> str: import module_name
"""Return a string representation of this day.
print(module_name.function(arguments))
>>> d = Day(20, 'December', 2022)
>>> d.schedule_event(event.Event(12, 13, 'Submit Final'))
>>> d.schedule_event(event.Event(14, 23, 'Celebrate!'))
>>> print(d)
20 December 2022:
import event
- Submit Final: from 12 to 13
- Celebrate!: from 14 to 23 print(event.Event(12, 13, ‘Submit Final’))
"""
result = ‘{0} {1} {2}:\n’.format(self.day, self.month, self.year)

for event in self.events:


result += ‘ - {0}\n’.format(event)

return result.strip

if __name__ == '__main__':

# Create day 21 December 2022.


day = Day(21, ‘December’, 2022)

# Add an event "Sleep in" from 0 to 11 on 21 December 2022.


e1 = event.Event(0, 11, ‘Sleep in’)
day.schedule_event(e1)

# Add an event "Brunch" from 11 to 13 on 21 December 2022.


e2 = event.Event(11, 13, ‘Brunch with friends’)
day.schedule_event(e2)

# Print the day 21 December 2022, including its events.


print(day)

You might also like