In Class: Week 12 Tuesday and Thursday:
Defining Classes
Create a week12 subdirectory in your
cs21/class directory by running update21:
$ update21
$ cd
$ cd cs21/class/week12
Topics
- Object-Oriented Programming
- Defining Classes
- Class methods
- The constructor, __init__
- String conversion, __str__
- The implicit self parameter
- Access methods a.k.a. getters
- Mutator methods a.k.a. setters
In-class work
Classes
- Open up accounts.py. Read through the mostly completed
class definition for a bank account. Using this class will be able
to define Account objects.
- First, we will illustrate the use of several functions. You should
be able to draw a stack diagram for the constructor showing how self
is defined.
- Open up testAccounts.py and trace the code which creates
three separate Account objects using the constructor, prints
them using the string conversion method, and prints out information given
by the accessor methods getBalance and getName
- Take some time to implement and test the changePin method. It
is very important that you employ unit testing just as you did with
the top-down design labs
- When you are finished, analyze and test the withdraw() function.
Once you understand how that works, implement and test the deposit()
method. If you have time, implement and test the computeInterest()
method.
- In song.py, begin implementing a Song class from scratch. This
class should define the necessary data to describe a Song on your computer
(e.g., and MP3). This includes such data as title, artist, album, length,
file name, etc. Also define the necessary methods, including getters and
setters, to manipulate the object. Be sure to test our your methods
one-by-one in your testSong.py program.
include("style/footer.php"); ?>