CS1010S - Programming Methodology Notes

Overview of CS1010S: Programming Methodology

  • Focuses on programming concepts with an emphasis on object-oriented programming (OOP).

Key Concepts in Class and Encapsulation

Abstract Data Types (ADT)
  • Definition: A mathematical model for data types in programming.

  • Student ADT Example:

    • Identified by a matric number.

    • Functions include:

    • make_student(matric, faculty, mark) creates a student instance.

    • Accessor Methods Methods that retrieve values of attributes without modifying them::

      • get_matric(student): Retrieves matric number.

      • get_faculty(student): Retrieves faculty.

      • get_mark(student): Retrieves mark.

    • Mutator Method: A method that modifies the data of an object:

      • set_mark(student, mark): Updates the mark of the student.

Implementation of Student ADT via Lists
  • Using Lists as the underlying structure:

  def make_student(matric, fac, mark):
      return [matric, fac, mark]
  • Accessors and Mutators Example:

  def get_matric(student):
      return student[0]
  def set_mark(student, mark):
      student[2] = mark
  • Problem: Can accidentally misuse functions due to similar structure.

Tagged List Implementation
  • Example with a tag to prevent misuse:

  def make_student(matric, fac, mark):
      return ['S', matric, fac, mark]
  • Accessor Method with Tag Check:

  def get_matric(student):
      if student[0] == 'S':
          return student[1]
Dictionary-Based Implementation
  • Improved method to reduce attribute conflicts:

  def make_student(matric, fac, mark):
      return {'tag': 'Student', 'matric': matric, 'fac': fac, 'mark': mark}
  • This method minimizes error through key-value pairs.

Class Syntax for OOP
  • A class encapsulates data and behavior:

  class Student():
      def __init__(self, matric, fac, mark):
          self.matric = matric
          self.fac = fac
          self.mark = mark
      # Accessor
      def get_matric(self):
          return self.matric
      # Mutator
      def set_mark(self, mark):
          self.mark = mark
  • Important concepts:

    • Keyword class is used to declare the class.

    • __init__ is the constructor method, automatically called to initialize new objects.

    • Attributes are prefixed with self to indicate they belong to the class instance.

Notes on Object-Oriented Programming (OOP)

  • OOP allows creation and use of objects with methods.

    • Encapsulation: Binding of data and behavior.

    • Inheritance: Classes can inherit from other classes.

    • Polymorphism: Ability to process objects differently based on their class.

  • Best Practices:

    • Create classes that have single, well-defined responsibilities, which promotes maintainability and scalability.

    • Avoid direct access to attributes; utilize methods for accessing or modifying them to ensure controlled interactions with the data.

Additional Features

  • Magic methods in Python enable operator overloading and other functionalities.

    • Examples include __add__, __sub__, and __str__ for mathematical and string representation operations.

  • Name Mangling for privacy helps to protect intended attributes by modifying attribute names in a way to avoid conflicts.