CS 240: Data Structures and Algorithms
James Madison University, Spring 2013

In-Class Activity #3: Object Oriented Python

Objectives

The goal of today's activity is to get comfortable working with object oriented features of Python by implementing a simple class hierarchy.

Resources

Most of what you need to know to complete today's activity is covered in appendix D of our textbook. The section on Classes in the Python tutorial is also quite clear.

You may also want to refer to the documentation for the datetime class from Python's standard library: http://docs.python.org/2/library/datetime.html. You should use this class to represent the date and time information in the classes below. Take a look at the file datetime_demo.py for an example of how the class is used.

Exercises

  1. Create a new Python file, and add a main function for testing the classes that you will create below. Don't forget to include a call to main.
  2. Implement the class hierarchy described by the UML diagram below. Before you start programming, think carefully about which data attributes and methods should be defined in which classes, and how best to take advantage of inheritance. Test your code as you work.



    Notes:
    • The colons in the diagram above are used to indicate types. day(): int indicates that the day method will return an integer value. conflicts(other:Event): bool indicates that the conflicts method expects an Event argument and returns a boolean. Python will not enforce these types and you don't need to enforce them either. In this case, the types are just indicating our expectations.
    • In Python it is necessary to explicitly call the constructor of the superclass in the the constructor of the subclass. The syntax is:
      super(SubClassName,self).__init__([ARGS])
      
      Where SubClassName is the name of the subclass, and [ARGS] represents the arguments expected by the constructor of the superclass (excluding self).

Finishing Up

There is nothing to hand in for this assignment. Make sure that you save a copy of your code. If you worked with a partner, make sure both of you get a copy.