Week 3: Object Collaboration in Programming

Sep 18, 2024

Lecture Notes - Week 3

Introduction

  • Good morning everyone.
  • We are officially entering week 3 of the course.
  • By now, students should be familiar with the software and class structure.

Recap of Weeks 1 & 2

  • Topics covered:
    • Procedural vs. Object-Oriented Programming
      • Focus on Object-Oriented Programming (OOP) for the current unit.
    • Key Concepts of OOP:
      • Objects:
        • Contain attributes (data/fields).
        • Perform methods (behaviors).
      • Classes:
        • Template for creating objects.
        • Must contain a constructor.
    • Constructor:
      • Same name as the class, no return type.
      • Used to create an object from the class.
    • Properties:
      • Access private fields of a class.
      • Important for encapsulation.
    • Methods:
      • Can have return types or be void (no return).

Unit Testing

  • Importance of unit tests to verify methods in a class.
  • Use of NUnit framework for testing in C#.
  • Essential packages to add for testing:
    • nunit.framework

Week 3 Focus: Object Collaboration

  • Learning about object collaboration in solving complex problems.
  • Example: Building a Monopoly Game
    • Identify main objects:
      • Die, Monopoly Game, Board, Player, Piece, Square.
    • Emphasizes multiple classes working together.
    • Relationship types between classes:
      • Association
      • Aggregation
      • Dependency

Class Diagram

  • High-level view of class relationships.
  • Example classes for Monopoly Game and their relationships.

Relationship Types

  1. Association:
    • Described as "has a" relationship (e.g., Instructor teaches Students).
    • Represented by a straight line.
  2. Aggregation:
    • "Is a part of" relationship.
    • Types:
      • Shared Aggregation:
        • Parts can exist independently (e.g., players in a team).
      • Composition:
        • Parts cannot exist without the whole (e.g., engine in a car).
  3. Dependency:
    • Describes usage (e.g., Player uses Die).
    • Represented by a dashed arrow.

Class Diagram Notation

  • Rectangle for class, divided into three segments:
    1. Class name
    2. Attributes
    3. Methods
  • Access modifiers:
    • Public: +
    • Protected: #
    • Private: -

Examples of Relationships

  • Person & Passport:
    • Analysis leads to a potential dependency relationship.
  • House & Room:
    • Composition relationship.
  • Airplane & Passenger:
    • Shared aggregation or association based on context.

Coding Examples

Course and Unit Classes

  • Unit Class:
    • Fields: Code, Name, Available.
    • Methods: Constructor, Print Details.

List vs. Array

  • Differences:
    • Array: Fixed size, cannot be resized.
    • List: Dynamic size, can grow/shrink as needed.
  • Example code for creating and managing lists in C#.

Unit Testing in C#

  • Process of setting up unit tests:
    1. Setup objects to test.
    2. Execute methods.
    3. Check results.
  • Example tests for adding/removing units from a course.

Summary

  • This week focused on understanding relationships between classes and object collaboration.
  • Importance of using the right relationship type based on the scenario.
  • Continuous practice and reviewing of previous weeks recommended.

Questions & Conclusion

  • Students encouraged to ask questions for clarity.
  • Reminder to review submissions and ensure all requirements are met.