Days 5-6: Collections and Object-Oriented Programming (OOP)

Days 5-6: Collections and Object-Oriented Programming (OOP)

Objective:

  • Learn list, set, map.

  • Start with basic OOP concepts: classes, objects, properties, methods.


1. Collections in Dart

1.1 List:

A list is an ordered collection of items. Lists in Dart are similar to arrays in other languages.

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  print('Numbers: $numbers');

  // Accessing elements
  print('First element: ${numbers[0]}');

  // Modifying elements
  numbers[1] = 10;
  print('Modified list: $numbers');

  // Adding elements
  numbers.add(6);
  print('List after adding element: $numbers');

  // Removing elements
  numbers.removeAt(2);
  print('List after removing element: $numbers');
}

Explanation:

  • A list is declared using square brackets [].

  • Elements are accessed using the index.

  • Elements can be modified, added, and removed.

1.2 Set:

A set is an unordered collection of unique items.

void main() {
  Set<String> fruits = {'Apple', 'Banana', 'Mango'};
  print('Fruits: $fruits');

  // Adding elements
  fruits.add('Orange');
  print('Set after adding element: $fruits');

  // Removing elements
  fruits.remove('Banana');
  print('Set after removing element: $fruits');

  // Checking existence
  print('Contains Mango: ${fruits.contains('Mango')}');
}

Explanation:

  • A set is declared using curly braces {}.

  • Elements in a set are unique and unordered.

  • Elements can be added, removed, and checked for existence.

1.3 Map:

A map is a collection of key-value pairs.

void main() {
  Map<String, int> ages = {
    'Alice': 30,
    'Bob': 25,
  };
  print('Ages: $ages');

  // Accessing values
  print('Alice\'s age: ${ages['Alice']}');

  // Modifying values
  ages['Bob'] = 26;
  print('Modified ages: $ages');

  // Adding entries
  ages['Charlie'] = 28;
  print('Ages after adding entry: $ages');

  // Removing entries
  ages.remove('Alice');
  print('Ages after removing entry: $ages');
}

Explanation:

  • A map is declared using curly braces {} with key-value pairs separated by colons :.

  • Values are accessed using keys.

  • Entries can be modified, added, and removed.


2. Basic OOP Concepts in Dart

2.1 Classes and Objects:

A class is a blueprint for creating objects. Objects are instances of a class.

class Person {
  String name;
  int age;

  // Constructor
  Person(this.name, this.age);

  // Method
  void introduce() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

void main() {
  // Creating objects
  Person alice = Person('Alice', 30);
  Person bob = Person('Bob', 25);

  // Calling methods
  alice.introduce();
  bob.introduce();
}

Explanation:

  • A class Person is defined with properties name and age.

  • A constructor is used to initialize the properties.

  • A method introduce is defined to print a greeting message.

  • Objects alice and bob are created from the Person class and the introduce method is called on each object.

2.2 Properties and Methods:

Properties are variables that belong to a class. Methods are functions that belong to a class.

class Car {
  String brand;
  int year;

  // Constructor
  Car(this.brand, this.year);

  // Method
  void displayInfo() {
    print('Car: $brand, Year: $year');
  }
}

void main() {
  // Creating objects
  Car car1 = Car('Toyota', 2020);
  Car car2 = Car('Honda', 2018);

  // Calling methods
  car1.displayInfo();
  car2.displayInfo();
}

Explanation:

  • A class Car is defined with properties brand and year.

  • A method displayInfo is defined to print the car's information.

  • Objects car1 and car2 are created and the displayInfo method is called on each object.

2.3 Getters and Setters:

Getters and setters are used to access and modify private properties.

class Rectangle {
  double _width;
  double _height;

  // Constructor
  Rectangle(this._width, this._height);

  // Getter for width
  double get width => _width;

  // Setter for width
  set width(double width) {
    _width = width;
  }

  // Getter for height
  double get height => _height;

  // Setter for height
  set height(double height) {
    _height = height;
  }

  // Method to calculate area
  double getArea() {
    return _width * _height;
  }
}

void main() {
  // Creating object
  Rectangle rect = Rectangle(5.0, 3.0);

  // Using getters
  print('Width: ${rect.width}');
  print('Height: ${rect.height}');

  // Using setters
  rect.width = 6.0;
  rect.height = 4.0;

  // Calculating area
  print('Area: ${rect.getArea()}');
}

Explanation:

  • A class Rectangle is defined with private properties _width and _height.

  • Getters and setters are defined to access and modify these properties.

  • A method getArea is defined to calculate the area of the rectangle.

  • An object rect is created and the getters, setters, and getArea method are used.


Visual Explanations

1. List:

  • Figure 1: Example of a list in Dart.

2. Set:

  • Figure 2: Example of a set in Dart.

3. Map:

  • Figure 3: Example of a map in Dart.

4. Classes and Objects:

  • Figure 4: Class and object diagram.

5. Getters and Setters:

  • Figure 5: Getters and setters example.

By following this guide, you'll have a solid understanding of collections and basic OOP concepts in Dart, enabling you to write more structured and efficient programs. Happy coding!