CS350: Data Structures Class Notes
Class 2: Wednesday, September 3, 2008
Announcements:
We are changing classrooms. We will now meet in Olin 212.
There are links to code discussed today on the class web page.
Abstract Data Types or ADT
When programming, we use modules/modularity to partition our code for three reasons:
⢠It simplifies the task of codingâwe can focus on individual task rather than the
whole problem
⢠It allows us to eliminate redundanciesâwe can reuse code
⢠It allows us to isolate errorsâthis simplifies debugging and unit testing
The key ideas are information hiding and encapsulation.
There are two parts to what we are doing. We want to separate the âwhatâ from the
âhowâ. Both must be addressed, but it is easier if we do this separately. The âwhatâ
defines the external interface. The âhowâ is the implementation.
An ADT is composed of two parts, the data and the operations on the data. Contrasted to
a data structure in a programming language, the data structure only include the data, not
the operations.
E.g., an ADT for Booleans would include: TRUE, FALSE, AND, OR, and NOT.
The correspondence between an ADT and a class definition in Python should be obvious.
Rational Numbers
Data (a/b)
numerator, denominator
Operations
Rationalâconstructor
numIsâaccesser function that returns the numerator
denIsâaccesser function that returns the denominator
+ or __add__âadds two rational numbers: a/b+c/dď (ad+bc)/bd
â or __sub__âsubtract two rational numbers: a/b-c/dď (adâbc)/bd
* or __mul__âmultiplication: a/b*c/dď ac/bd
/ or __div__âdivision: a/b / c/d ď ad/bc
<, ==, >, etc. or __cmp__âcomparison â1 for less-than, 0 for equal, +1 for greater-than
Other issues including printing, etc.