Week 7 - Designing and Programming Classes/Objects

Design Activity

Last week we spent the week reading and watching videos trying to develop the concept of what goes in to a Class definition.  Primarily, we talked about the fact that an Object has three things:

To start this week I want you to think about the design of a couple of different classes.

With a partner if you want

When we worked with Scratch we were programming Sprites. It is very likely that each sprite you programmed was an instance of some form of "Sprite" class.  Let's assume that they actually are and think about what the design for these would look like. 

  1. What data does each individual sprite know about itself? Think about this carefully.  I think you should be able to come up with no fewer than six pieces of data.  When I thought about it for a while I came up with six obvious things, two mostly obvious things, and 6-12 more obscure things.  Clearly your definition of "obvious", "mostly obvious" and "obscure" may differ from mine.  But with a partner see how many things you can come up with.
  2.  

  3. Now, we should think about the "default" values that these instance variables get when new sprites are created. For this part let's only worry about the six instance variables that I labeled as "obvious" in my solution posted in step #1.1.  What are the default values given to these when a new sprite is created.

 

  1. Finally, what actions can each sprite perform.  Again, this can get fairly large fairly quickly.  So let's only focus on those actions in Scratch that manipulate the six values I labeled as "obvious".

 

Code Activity

Last week I showed you working code for two classes:

While we will not make a habit of having you write classes in this course - mostly just using them - I think it is worth TRYING to write a class from scratch.  In fact, let's have you write a Python version of the Scratch Sprite that we designed together last week.

Using the CreditCard and Fraction examples from above, create a Python Class called Sprite which you save in a file named Sprite.py.  This class should provide all of the data and functionality that was outlined in the class diagram shown below:

Most of these are fairly straight forward because they are simple operations.  But a few of them have some error checking and one even has (heaven forbid) trigonometry. And non-standard trigonometry at that.  Since my MAIN learning outcome with this is really just to get you to explore with the structure of Python class code, I will give you the option to create one of two versions.

 

A class called SimpleSprite in a file called SimpleSprite.py

Once you write your Sprite you can download this tester program and run it to see if your outcomes agree with what I was expecting.

 

A class called Sprite in a file called Sprite.py

Once you write your Sprite you can download this tester program and run it to see if your outcomes agree with what I was expecting.