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
Personis defined with propertiesnameandage.A constructor is used to initialize the properties.
A method
introduceis defined to print a greeting message.Objects
aliceandbobare created from thePersonclass and theintroducemethod 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
Caris defined with propertiesbrandandyear.A method
displayInfois defined to print the car's information.Objects
car1andcar2are created and thedisplayInfomethod 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
Rectangleis defined with private properties_widthand_height.Getters and setters are defined to access and modify these properties.
A method
getAreais defined to calculate the area of the rectangle.An object
rectis created and the getters, setters, andgetAreamethod 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!


![[Indonesian] Memahami Wiki AW Non-Commercial License 1.0](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F6608de5cd660c76b8626061d%2Ff2bf3812-7079-4c4f-a6f9-fe2ba1c0aa06.png&w=3840&q=75)


![[Indonesian] 13 Organ HODi (Hybrid Omnichannel Distribution)](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1753044716865%2F77ddf777-bac7-4d0b-882b-1273ddaf2adf.png&w=3840&q=75)