Session 2 - Intro to 🧑💻programming with Dart
Chapter 4: Switch Statement
In his journey, Ikem stumbled upon a village where the locals were troubled by a mischievous spirit that changed the weather unpredictably. Ikem took it upon himself to create a program to help the villagers predict the weather.
COPY
// Ikem's Adventure: Taming the Unpredictable with Switch Statement
void main() {
// Ikem encounters a challenge in predicting the weather.
String weather = 'sunny'; // Current weather condition
// Ikem analyzes the weather and provides a forecast.
switch (weather) {
case 'sunny':
print('The sun is shining bright. It\'s a beautiful day!');
break;
case 'cloudy':
print('The sky is covered with clouds. Expect some rain.');
break;
case 'rainy':
print('Raindrops are falling gently from the sky. It\'s a rainy day.');
break;
default:
print('The weather is unpredictable. Be prepared for anything!');
}
// Ikem's quest to predict the weather continues...
}
And so, with each step of his journey, Ikem grew in skill and knowledge, drawing ever closer to his goal of becoming a legendary coder. Armed with the wisdom of variables, the power of conditionals, the mastery of loops, and the versatility of switch statements, Ikem's adventure was just beginning...
Chapter 5: For Loop and Iteration
In his travels through the vast expanse of CodeIgba, Ikem stumbled upon a bustling marketplace where merchants traded goods from distant lands. Intrigued by the array of exotic wares on display, Ikem decided to explore the market and learn more about its inhabitants.
COPY
// Ikem's Adventure: Exploring the Marketplace with For Loop
void main() {
// Ikem arrives at the bustling marketplace.
List<String> items = ['apples', 'bananas', 'oranges', 'grapes', 'kiwis']; // Array of items for sale
// Ikem explores the market and interacts with the merchants.
for (String item in items) {
print('Ikem examines a basket of $item.');
print('Merchant: "Fresh $item for sale! Get your $item here!"');
}
// Ikem's curiosity leads his to discover new treasures in the marketplace.
}
As Ikem wandered through the market stalls, she marveled at the diversity of goods on offer and engaged in lively conversations with the merchants. With each step, she gained valuable insights into the power of the for loop, which allowed his to iterate over the array of items and interact with each merchant in turn. And thus, his journey of discovery continued, fueled by the endless possibilities that awaited his in the world of coding.
Curtain closes
Assignment
Thats the end of this session
OOP (Object oriented Programming) in Dart
Once upon a time in the land of CodeIgba, there was a legendary programmer named Ikem, renowned for their mastery of Object-Oriented Programming (OOP) principles. Ikem embarked on a journey through the vast forests of Dartland to teach aspiring developers the art of OOP in Dart using the analogy of shapes, a calculator, and an ATM system.
The Story of Ikem the Programmer:
Chapter 1: Shapes
Ikem encountered a village where the inhabitants spoke of geometric shapes. To explain classes and objects, Ikem created a Shape class with properties like color and methods like calculateArea()
.
//dart
class Shape {
String color;
Shape(this.color);
double calculateArea() { return 0.0; }
}
class Circle extends Shape {
double radius;
Circle(String color, this.radius) : super(color);
@override double calculateArea() {
return 3.14 radius radius;
}
}
void main() {
var myCircle = Circle("red", 5); print("Area of the circle: ${myCircle.calculateArea()}"); }
}
Chapter 2: Calculator
In another town, Ikem illustrated inheritance by creating a Calculator class with basic arithmetic operations.
//dart
class Calculator {
double add(double x, double y) => x + y;
double subtract(double x, double y) => x - y;
}
void main() {
var myCalc = Calculator();
print("Addition: ${myCalc.add(5, 3)}");
print("Subtraction: ${myCalc.subtract(5, 3)}");
}
Chapter 3: ATM System
As Ikem ventured further, they explained encapsulation and abstraction using an ATM system.
//dart
class ATM {
double _balance = 0;
void deposit(double amount) {
_balance += amount; print("Deposited: $${amount}");
}
void withdraw(double amount) {
if (_balance >= amount) {
_balance -= amount;
print("Withdrawn: $${amount}");
} else {
print("Insufficient funds.");
}
}
double checkBalance() {
return _balance;
}
}
void main() {
var myATM = ATM();
myATM.deposit(1000);
myATM.withdraw(500);
print("Current Balance: $${myATM.checkBalance()}");
}
And so, Ikem journey through Codeopolis enlightened many on the principles of OOP in Dart, leaving a legacy for generations of programmers to come.
Subscribe to my newsletter
Read articles from Ucheolisah Philips Nge directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by