Post-test: Review vignettes on:
Abstract Data Types
Concept of Encapsulation
Encapsulation, the Gearbox example
Stacks
Discussion points:
Abstract Data Types (ADTs)
Data structures and their implementation
Creating data types independent of implementation details
Describing properties and operations of data types
Familiar data types: Integers, Strings, Booleans
Accessing data operations:
In Python, use square bracket notation (e.g., myList[i]
) to access list elements
Selecting a variable type defines:
Possible values for that variable
Operations applicable on it
Meaning of the data
Need to define complex data structures and enhance existing data types
Common operations on a datetime type include:
Replacing year, month, day, time zone
Retrieving year, month, day, hour, minute, weekday
Conversion to string
Date manipulation using datedelta()
and timedelta()
Details on datetime package not critical to the current stage.
Software evolution requires new data structures to handle updated requirements
Changes to programs often involve modifying existing data structures (e.g., adding fields to personnel records)
Changes in data structures or behavior (interface) can lead to high maintenance costs
Minimize impact of changes by separating data structure implementation from program implementation
Avoid rewriting procedures that use the altered data structure
Definition: Abstraction hides details of data structure implementation, exposing only possible operations (interface)
Encapsulation allows accessing data only via the interface, not directly.
Usage analogy:
Driving a car: Understanding use without knowing engine mechanics
Using a calculator: Operations without knowing internal computations
Operating a phone: Knowing how to function without understanding inner workings
Floating point arithmetic is an ADT; users need not understand its implementation
Encapsulation hides implementation details from users; focus on operators like +, -, *, /
Python's built-in list functions without needing to understand their implementation:
list.append(x)
: Add item to end
list.extend(L)
: Append items from list L
list.insert(i, x)
: Insert item at position i
Other operations: remove()
, pop()
, index()
, count()
, sort()
, reverse()
Definition: ADT comprises objects sharing properties and behaviors
Properties represent internal data state (e.g., double d
)
Behaviors represent operations on data (e.g., sqrt(d) / 2
)
Focus on OOP principles: data abstraction
ADTs serve as formal descriptions, independent of programming languages
Benefits include promoting design by contract, specifying responsibilities for suppliers and clients
Bags: Can have duplicates, allow addition, removal, access, no order
Sets: Similar to bags but do not allow duplicate elements; can perform union, intersection, difference, subset operations
Definition: Collection with access only to the last element inserted (Last In - First Out)
Operations:
Push: Add an element
Pop: Remove the element from the top
Example of usage: Browser history management with back button
Additional structures:
Lists
Queues
Deques
Trees
Heaps
Binary search trees
Maps
Graphs
ADTs are specifications that cannot be used directly in programming
Implementation of an ADT is called a Data Structure
In OOP, create a new class to implement an ADT
Definition: Implementation of an ADT and organization of information in memory for better efficiency
Data Structures as containers: hold data organized for specific operations
Linear data structures: collections with ordered items
Different structures optimize for different operations, defined by how items are managed
Essential operations of Data Structures:
Add items
Remove items
Access items
Operation details vary based on Data Structure type (e.g., List)
Mutator: Controls changes to a variable (setter)
Sets a value, validates it, and updates the variable
Accessor: Returns the value of a variable (getter)
Provides data without alteration
Usage of templates and instances from classes:
List instance: Functions include show()
, update(word, character)
Integer instance: Functions include increment()
, hangman()
Modern languages include libraries for data structures:
Java: Collections framework
C++: Standard Template Library
Python: Lists, tuples, sets, dictionaries
Lisp: Lists
Objects exhibit:
Private state
Public protocol for interaction and resource access
Every object is an instance of a class; shared protocol among instances
State of an object differentiates instances of the same class
Avoid exposing member properties directly through getters and setters
Ensure public interface methods modify or retrieve data without revealing implementation details
Interface knowledge is available outside the class, but data structure implementation remains hidden.