Project: Overlapping Appointments#
Agenda is our first project with user-written classes.
Overview#
In this project you will
Create a class
Appt(short for “appointment”) that represents a single appointment with a start time, an end time, and a description. For example, you might create anApptobject to represent CS 210 lecture from 10am to 11:20am on January 7, 2025.The
Apptclass will make use of thedatetimeclass from the Python library, which you will import.Each
Apptobject will contain three instance variables, also known as fields. Two of these, the start time (e.g., 10:00am on January 7, 2025) and end time (e.g., 11:20am on January 7) will be represented bydatetimeobjects. The third instance variable will be a description represented as astrobject.You will write several methods for the
Apptclass. These include magic methods for comparingApptobjects. We will interpret<as before, so thatapp1 < app2will mean “the end of app1 is no later than the beginning of app2”.You will write an
overlapsmethod.app1.overlaps(app2)will mean that there is a non-zero overlap in the periods represented byapp1andapp2.You will write an
intersectmethod, which creates a newApptobject representing the overlapping portion of two appointments.You will write additional methods for creating and printing
Apptobjects.
Create a class
Agendarepresenting a collection ofApptobjects. AnAgendaobject will be a wrapper for alistobject. It will have a single instance variable, alistofApptobjects. Many of its methods, likeappend, will be delegated to corresponding methods of thelistit wraps. The point of creating anAgendaclass is to alter the behavior of some methods (e.g., customizing the way a list of appointments is printed) and add some new methods.You will write a
conflictsmethod to detect any overlap in twoAgendaobjects. The result ofag1.conflicts(ag2)will be anotherAgendaobject, containing anApptobject for overlap between anyApptobject inag1and someApptobject inag2.
Most of the Agenda project is not complicated
or difficult, but you can
expect some frustration and time spent because you are using
unfamiliar language features. Be sure to correctly use
the methods you have defined to simplify other parts of the program
(the DRY principle, don’t repeat yourself). For example, when
you write the overlaps method of Appt, make use of the < magic
method by noting that if app1.overlaps(app2), then neither
app1 < app2 nor app2 < app1.
The only tricky bit in the project is making the intersect
method efficient. By sorting both Agenda objects ag1 and ag2,
you can avoid comparing every Appt in ag1 with every Appt
in ag2, which would take time proportional to the product of their
lengths.
You will turn in one file, appt.py, containing both class Appt
and class Agenda.