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 anAppt
object to represent CS 210 lecture from 10am to 11:20am on January 7, 2025.The
Appt
class will make use of thedatetime
class from the Python library, which you will import.Each
Appt
object 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 bydatetime
objects. The third instance variable will be a description represented as astr
object.You will write several methods for the
Appt
class. These include magic methods for comparingAppt
objects. We will interpret<
as before, so thatapp1 < app2
will mean “the end of app1 is no later than the beginning of app2”.You will write an
overlaps
method.app1.overlaps(app2)
will mean that there is a non-zero overlap in the periods represented byapp1
andapp2
.You will write an
intersect
method, which creates a newAppt
object representing the overlapping portion of two appointments.You will write additional methods for creating and printing
Appt
objects.
Create a class
Agenda
representing a collection ofAppt
objects. AnAgenda
object will be a wrapper for alist
object. It will have a single instance variable, alist
ofAppt
objects. Many of its methods, likeappend
, will be delegated to corresponding methods of thelist
it wraps. The point of creating anAgenda
class 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
conflicts
method to detect any overlap in twoAgenda
objects. The result ofag1.conflicts(ag2)
will be anotherAgenda
object, containing anAppt
object for overlap between anyAppt
object inag1
and someAppt
object 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
.