< Back

Lecture 7- Object Oriented Design Redo


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

$ mkdir ~/cs170/labs/lab7 
$ cd ~/cs170/labs/lab7

We tried to practice a little bit on Friday, but it seemed to have backfired. So I figured today would be a wonderful day to make sure we all understand how useful classes and objects are before we continue to some more challenging aspects of object oriented design.


Lab Assignment 7
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

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.

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 (which is the direct average of the students quiz average and assignment average).

 

Challenge

One 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.

Lab Activity
Drive

Up to now, I've been making you write classes that mostly just stored data. While this is incredibly common and useful, it's probably a little less interesting than other tasks we've done in CPSC 120 and 170. So to close today, I figured we could write a class that does something a bit more concrete.

Details

Create a class called ModelTk, which will represent a very simple looking car to be drawn on a tkinter canvas. While you can be a little creative with what the car looks like, it at least needs to have two wheels, and the body of the car needs to be made of at least two other shapes. See the image below for an example.

Create a file called drive.py, which creates a tkinter window and creates at least one car. You should be able to move the car using the 'a' and 'd' keys.

Think about how you can simplify your tasks by adding methods or attributes to your class. Attempt to design your classes and files to be as easy to use as possible.

Example

Hint

  • Your car class needs at least 3 attributes: an x and y coordinate and the canvas the car is to be drawn on.

  • Your car class should have a method called draw_car, which draws the car on the canvas at its current location on the canvas.

  • It may be easier to write a method called move_car, which takes a KeyPress event as a parameter. It should update the appropriate attribute of the car necessary, and clear and redraw the car to the window.

  • Your car class may benefit from having an additional attribute which is a list of the id's of the elements drawn on the TK window. You can then use these id's to delete from the canvas upon an update.

 

Challenge

It's time for a drag race! The regular portion of this activity is to let the user control the car. Instead, create two cars which have some randomly generated speed. Animate these cars racing from one edge of the screen to the other.


Submission

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

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

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


In-Class Notes