Lecture 6 - Classes and Objects


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

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

More Practice with Classes and Objects

You've now been required to write two classes. The key detail about writing classes is that practice is the best way to get better at them. The more experience you have, the better your intuition will be when writing classes. So instead of lecturing today, you're going to spend the day writing at least 2 classes.


Lab Assignment 6
Gradebook

As many of you have likely noticed, I'm not an incredibly big fan of Inquire. However, The one part I do like about Inquire is the gradebook. It is much nicer than having a spreadsheet storing grades. However, I'd like to break away from using Inquire at some point. I could easily write my own gradebook software, but I think it might be better practice for you to write one.

Details

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

You are going to write a simple gradebook program for CPSC 170. Towards that end, you need to write two different classes: Students and Gradebook. Your Student class should store all of the things associated with a student, namely quiz and assignment grades.

Your Gradebook class is simply going to be a collection of students. I should be able to get a list of all the quiz grades of a particular student, as well as get a list of all of the assignment grades for a particular student. I also might want to be able to add new students, and add new grades to a particular student.

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

>>> my_gradebook = Gradebook()
>>> my_gradebook.add_student("Thomas")
>>> my_gradebook.add_student("Maya")
>>> my_gradebook.add_assignment_grade("Maya", 100)
>>> my_gradebook.add_assignment_grade("Thomas", 95)
>>> my_gradebook.add_assignment_grade("Thomas", 100)
>>> my_gradebook.get_assignment_grade("Thomas")
[95, 100]
>>> my_gradebook.add_quiz_grade("Thomas", 90)
>>> my_gradebook.get_average("Thomas")
95.0
>>> 

Hint

  • Your Student class should have attributes for name, assignment_grades, and quiz_grades. The first one will be a string, while the latter two should be lists.

  • Your Gradebook needs to be a collection of students. A list would definitely work, but there may be another datastructure you can use to ease the writing of subsequent methods.

  • Your Student Class should have methods get_average, print_quiz_grades, and print_assignment_grades. They should behave as you would expect them to.

  • Your Gradebook should have methods get_quiz_grades and get_assignment_grades, which take a string representing a Student's name, and returns a list of that sudents grades.

  • The Gradebook needs methods add_assignment_grade, add_quiz_grade, which takes both a string representing a student's name and an integer representing that students grade. It should update the associated student object appropriately, which probably means the Student class should have those as well.

  • Your gradebook should also have a method called get_average, which takes a string representing the name of a student, and returns that students current average.

 

Challenge

So you have a class that can represent a gradebook, but currently the only way to use it is to either write your own program or start the python interactive interpreter. Write a (text-based) menu-driven system that allows a user to add students, add grades, view grades, etc.

 

Challenge

Another drawback to the current system is that grades go away once the program is quit. It would be nice to be able to store the current state of the gradebook, 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 cseval.roanoke.edu. You should see an available assignment called Lab Assignment 6. Make sure you include a header listing the authors of the file.


Last modified: Fri Jan 24 09:05:46 EST 2014