< Back

Lecture 6- Object Oriented Design


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

$ mkdir ~/cs170/labs/lab6 
$ cd ~/cs170/labs/lab6

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 Assignment 6
Planner

If you ask anyone out of my Web Programming class from last semester, I have a soft spot in my heart for To-Do lists. 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

$ python3 date_planner.py
Welcome to my planner!

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 0

What month? Jan
What day? 23
What year? 2015
What title? CPSC 170A

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 1

Jan 23 2015
=============
CPSC 170A

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 0

What month? Jan
What day? 23
What year? 2015
What title? Dinner

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 1

Jan 23 2015
=============
CPSC 170A

Jan 23 2015
=============
Dinner?

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 2

What event? Dinner?

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 1

Jan 23 2015
=============
CPSC 170A

Options:
0) Add a new event
1) Print current events
2) Delete event
3) Exit Program

What is your choice? 3
$

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.

  • 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 perform 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 lab6 directory. To create a tar file, execute the following commands:

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

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


In-Class Notes