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

I am a developer from Indonesia
Search for a command to run...

I am a developer from Indonesia
No comments yet. Be the first to comment.
Exploring Shortcut Routes to Professionalism: Delving into Dart, Flutter, and Serverpod for Building Stunning Applications. Github repository : https://github.com/ahliweb/learning-dart
Objective: Learn inheritance, polymorphism, abstraction, and encapsulation. Practice by creating small projects based on OOP concepts. 1. Inheritance Inheritance allows a class to inherit properties and methods from another class. // Parent clas...
Dokumen Nilai, Arah Hidup, dan Pedoman Implementasi 1. Pendahuluan Prinsip Hidup AhliWeb merupakan fondasi nilai yang menjadi arah berpikir, bersikap, mengambil keputusan, bekerja, membangun bisnis,
![[Indonesian] Prinsip Hidup AhliWeb Versi 2](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F6608de5cd660c76b8626061d%2F8a6aa718-4fc2-493c-86ad-5f26b1dcaf99.png&w=3840&q=75)
Pendahuluan AW Non-Commercial License 1.0 adalah lisensi perangkat lunak source-available yang dirancang untuk memungkinkan akses publik terhadap source code sekaligus tetap menjaga kontrol eksklusif
![[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)
Introduction The AW Non-Commercial License 1.0 is a source-available software license designed to allow public access to source code while preserving the copyright holder’s exclusive control over comm

Indonesia menghadapi tantangan besar di ruang digital, di mana jumlah pengguna media sosial telah melebihi 191,4 juta jiwa per Februari 2022, mencakup sekitar 68,9% dari populasi. Peningkatan sirkulasi konten ini, sayangnya, disertai dengan risiko ti...

🔰 RINGKASAN SINGKAT STRATEGI MARKETING HODi: HODi (Hybrid Omnichannel Distribution Initiative) adalah strategi distribusi dan pemasaran terpadu yang menggabungkan kekuatan online + offline, otomatisasi + human touch, teknologi + komunikasi personal...
Learn list, set, map.
Start with basic OOP concepts: classes, objects, properties, methods.
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.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.
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!