< Back

Lecture 5- Object Oriented Design


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab5 
$ cd ~/cs170/labs/lab5

One of the most difficult things you will be asked to do this semester is to create a class completely from scratch. I can give you hints on what attributes and methods to create, but ultimately you are in charge of your own destiny. As such, the only way to really get good at designing classes is through practice. I'll give you a chance to do that today.


Lab Activity 1
Planner

One thing I've noticed during my tenure here at Roanoke is that students are sometimes a little bit forgetful. They forget homeowrk assignments, how to submit them, etc. A planner is one way they get around that issue. The nice thing about planners is they are pretty efficient to write, and they can be incredibly useful with time management.

Details

Read this entire section before you start coding. There is a paper portion for this first!

You are going to write a simple date planner program for yourself. Towards that end, you need to write two different classes: Event and Planner. Your Event class should store all of the things associated with an event, namely a date (month, day, and year) and a title.

Your Planner class is simply going to be a collection of Events. I should be able to see a list of all the events in the Planner, as well as the ability to mark events as completed (which essentially is removing the event from the list). I also might want to be able to add new events.

Start by designing your classes on paper! Have either myself or one of the lab assistants look at this before you start writing. You should try to do this step without looking at the examples or hints below. Think about how you might be able to simplify your code, by putting methods in their appropriate places. Just because I listed an action above (or below, in the example) with a particular class doesn't mean you can't simplify things by writing additional methods in other classes.

Example

Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
1
Month: Feb
Day: 1
Year: 2016
Title: CPSC 170A
Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
1
Month: Feb
Day: 1
Year: 2016
Title: CPSC 425A
Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
2
Feb 1 2016
===============
CPSC 170A

Feb 1 2016
===============
CPSC 425A


Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
1
Month: Feb
Day: 2
Year: 2016
Title: Lunch?
Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
2
Feb 1 2016
===============
CPSC 170A

Feb 1 2016
===============
CPSC 425A

Feb 2 2016
===============
Lunch?


Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
3
Title: CPSC 425A
Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
2
Feb 1 2016
===============
CPSC 170A

Feb 2 2016
===============
Lunch?


Options:
1) Add Event
2) Print Events
3) Delete Events
4) Quit
4

Hint

  • Your Event class should have attributes for title, month, day, and year. The first two will be strings, while the latter two should be integers.

  • Your Planner needs to be a collection of Events. A list would definitely work well for this. This might be the only attribute you need for the Planner class.

  • Your Planner should have methods add_event and delete_event. add_event needs parameters for each attribute in Event. delete_event should only need the title of the event to remove.

  • Both of your classes should have the __str__ method defined, to make output easier.

  • You will also need a method akin to perform_menu which performs the menu driven portion of this assignment.

 

Challenge

One drawback to the current system is that events go away once the program is quit. It would be nice to be able to store the current state of the planner, but we seem to be hitting this problem alot. Luckily, Python has a wonderful module known as Pickle, which helpfully solves this problem for us. Now that we have written our own classes, we can take full advantage of it!

Take a look at the Pickle documentation. Your program should read the pickled file at the beginning of the program, and write back to the file whenever you quit.


Submission

When you have finished, create a tar file of your lab5 directory. To create a tar file, execute the following commands:

cd ~/cs170/labs
tar czvf lab5.tgz lab5/

To submit your activity, go to inquire.roanoke.edu. You should see an available assignment called Lab Assignment 5. Make sure you include a header listing the authors of the file.


In-Class Notes