Lab task 5:
Question no1:
void greet({ String? name, int? age = 0, String? city = 'Unknown'}) {
print('Hello, $name! You are $age years old and live in $city.');
}
void main() {
greet(name: 'Alia', age: 25, city: 'New York');
greet(name: 'Hamza');
greet(name: 'Ali', city: 'Los Angeles');
}
Output:
Question no2:
double calculateA(double a, double b) => (a * a) + (b * b * b * b);
double calculateZ(double p, double t, double A) => (p * p) + (5 * t) + A;
void main() {
print(calculateA(3.0, 2.0));
print(calculateZ(4.0, 5.0, 3.0));
Output:
Question no3:
double calculateA(double x, double p, double Z) => (x * x) + (2 * x * x)
+ (p * Z);
double calculateZ(double a, double B, double b) => (a * a) + (4 * B * B)
- (8 * b) + (2 * a);
double calculateB(double n, double q) => (n * n) + (q * q) + 1;
void main() {
print(calculateA(3.0, 2.0, 4.0));
print(calculateA(2.0, 3.0, 5.0));
print(calculateB(4.0,5.0));
Output:
Question no4:
double calculateZ(double x, double y, double p, double q) {
double calculateN() => (p * p) + (q * q);
return (x * x) + (4 * y * y) - (8 * calculateN());
}
void main() {
print(calculateZ(3.0, 2.0, 5.0, 2.0));
}
Output:
Question no5:
void main() {
List<String> fruits = ['apples', 'bananas', 'oranges'];
var modifiedFruits = fruits.map((fruit) => '${fruit.toUpperCase()}$
{fruit.substring(1)} is delicious');
modifiedFruits.forEach((fruit) => print(fruit));
Output:
Question no6:
typedef Operation = double Function(double a, double b);
double add(double a, double b) => a + b;
double subtract(double a, double b) => a - b;
double multiply(double a, double b) => a * b;
double divide(double a, double b) {
if (b == 0) {
print("Cannot divide by zero.");
return double.nan;
}
return a / b;
}
double calculate(double a, double b, Operation operation) => operation(a,
b);
void main() {
double x = 10;
double y = 5;
print('Addition: ${calculate(x, y, add)}');
print('Subtraction: ${calculate(x, y, subtract)}');
print('Multiplication: ${calculate(x, y, multiply)}');
print('Division: ${calculate(x, y, divide)}');
}
Output:
Question no7:
void main() {
List<Map<String, String>> myArray = [
{'name': 'ali', 'age': '45'},
{'name': 'noman', 'age': '34'},
];
print('Keys and Values:');
myArray.forEach((element) {
element.forEach((key, value) {
print('$key: $value');
});
});
print('\nValues Only:');
myArray.forEach((element) {
print(element.values);
});
}
Output:
Question no8:
void main() {
var myArray1 = [3, 4, 5];
var myArray2 = [6, 7, 8];
myArray1.addAll(myArray2);
print(myArray1);
}
Output:
Question no9:
void main() {
double calculateArea(double length, double width) {
double calculatePerimeter() => 2 * (length + width);
print('Perimeter: ${calculatePerimeter()}');
return length * width;
}
double length = 5.0;
double width = 3.0;
double area = calculateArea(length, width);
print('Area: $area');
}
Output:
Question no 10:
class Employee {
int id;
String name;
int age;
Employee(this.id, this.name, this.age);
void displayDetails() {
print('ID: $id, Name: $name, Age: $age');
}
}
class Staff extends Employee {
double salary;
String hireDate;
List<Map<String, dynamic>> completedProjects;
Staff(id, String name, int age, this.salary, this.hireDate,
this.completedProjects)
: super(id, name, age);
double averagePerformanceScore() {
if (completedProjects.isEmpty) return 0.0;
double totalScore = completedProjects.fold(0.0, (sum, project) => sum
+ project['performance_score']);
return totalScore / completedProjects.length;
}
}
class Manager extends Employee {
double salary;
String position;
String department;
List<Map<String, String>> teamMembers;
Manager(int id, String name, int age, this.salary, this.position,
this.department, this.teamMembers)
: super(id, name, age);
void displayTeamMembers() {
print('Team members of Manager $name in department $department:');
teamMembers.forEach((member) {
print('ID: ${member.keys.first}, Name: ${member.values.first}');
});
}
}
Employee? searchById(List<Employee> employees, int id) {
return employees.firstWhere((employee) => employee.id == id);
}
List<Staff> listHighPerformingStaff(List<Staff> staffList) {
return staffList.where((staff) => staff.averagePerformanceScore() >
4.5).toList();
}
List<Manager> managersInDepartment(List<Manager> managers, String
department) {
return managers
.where((manager) => manager.department == department)
.toList()
..sort((a, b) => b.salary.compareTo(a.salary));
}
void main() {
List<Employee> employees = [];
Staff staff1 = Staff(1, 'Ali', 30, 50000, '2020-01-15', [
{'project_name': 'Project A', 'performance_score': 4.8},
{'project_name': 'Project B', 'performance_score': 4.6},
]);
Staff staff2 = Staff(2, 'Noman', 25, 45000, '2021-03-10', [
{'project_name': 'Project C', 'performance_score': 4.2},
{'project_name': 'Project D', 'performance_score': 4.7},
]);
Staff staff3 = Staff(3, 'Sara', 28, 55000, '2019-11-05', [
{'project_name': 'Project E', 'performance_score': 4.9},
]);
employees.add(staff1);
employees.add(staff2);
employees.add(staff3);
// Creating Manager members
Manager manager1 = Manager(4, 'John', 40, 70000, 'Team Lead', 'IT', [
{'1': 'Ali'},
{'2': 'Noman'},
]);
Manager manager2 = Manager(5, 'Jane', 35, 80000, 'Department Head',
'Finance', [
{'3': 'Sara'},
]);
employees.add(manager1);
employees.add(manager2);
// Display details of all employees
print('Employee Details:');
employees.forEach((employee) => employee.displayDetails());
// Search for an employee by ID
int searchId = 2;
Employee? foundEmployee = searchById(employees, searchId);
if (foundEmployee != null) {
print('\nFound Employee:');
foundEmployee.displayDetails();
} else {
print('\nEmployee with ID $searchId not found.');
}
// List staff members with a performance score greater than 4.5
List<Staff> highPerformingStaff =
listHighPerformingStaff(employees.whereType<Staff>().toList());
print('\nHigh Performing Staff:');
highPerformingStaff.forEach((staff) {
print('${staff.name} with average performance score: $
{staff.averagePerformanceScore()}');
});
// Display managers in a specific department sorted by salary
String departmentToCheck = 'IT';
List<Manager> itManagers =
managersInDepartment(employees.whereType<Manager>().toList(),
departmentToCheck);
print('\nManagers in $departmentToCheck department (sorted by
salary):');
itManagers.forEach((manager) {
print('${manager.name} - Salary: \$${manager.salary}');
manager.displayTeamMembers();
});
}
Output:
Question no 11:
class Vehicle {
int vehicleId;
String make;
int modelYear;
Vehicle({
required this.vehicleId,
required this.make,
required this.modelYear,
});
void displayDetails() {
print('Vehicle ID: $vehicleId, Make: $make, Model Year: $modelYear');
}
}
class Car extends Vehicle {
double mileage;
String fuelType;
List<Map<String, dynamic>> serviceRecords;
Car({
required int vehicleId,
required String make,
required int modelYear,
required this.mileage,
required this.fuelType,
required this.serviceRecords,
}) : super(vehicleId: vehicleId, make: make, modelYear: modelYear);
double totalServiceCost() {
return serviceRecords.fold(0.0, (total, record) => total +
(record['cost'] as double));
}
}
class Truck extends Vehicle {
double payloadCapacity;
int axles;
List<Map<String, String>> deliveries;
Truck({
required int vehicleId,
required String make,
required int modelYear,
required this.payloadCapacity,
required this.axles,
required this.deliveries,
}) : super(vehicleId: vehicleId, make: make, modelYear: modelYear);
void displayDeliveries() {
print('Deliveries for Truck ID: $vehicleId');
deliveries.forEach((delivery) {
delivery.forEach((deliveryId, destination) {
print('Delivery ID: $deliveryId, Destination: $destination');
});
});
}
}
Vehicle? searchVehicleById(List<Vehicle> vehicles, int vehicleId) {
return vehicles.firstWhere((vehicle) => vehicle.vehicleId ==
vehicleId);
}
List<Car> listHighMileageCars(List<Car> cars) {
return cars.where((car) => car.mileage > 100000).toList();
}
List<Truck> trucksByPayloadCapacity(List<Truck> trucks, double
payloadCapacity) {
return trucks
.where((truck) => truck.payloadCapacity == payloadCapacity)
.toList()
..sort((a, b) => b.axles.compareTo(a.axles));
}
void main() {
List<Vehicle> vehicles = [];
Car car1 = Car(
vehicleId: 1,
make: 'Toyota',
modelYear: 2015,
mileage: 120000,
fuelType: 'Petrol',
serviceRecords: [
{'service_date': DateTime(2022, 5, 1), 'cost': 200.0},
{'service_date': DateTime(2023, 5, 1), 'cost': 250.0},
],
);
Car car2 = Car(
vehicleId: 2,
make: 'Honda',
modelYear: 2018,
mileage: 90000,
fuelType: 'Diesel',
serviceRecords: [
{'service_date': DateTime(2022, 4, 1), 'cost': 150.0},
],
);
Car car3 = Car(
vehicleId: 3,
make: 'Ford',
modelYear: 2012,
mileage: 110000,
fuelType: 'Petrol',
serviceRecords: [
{'service_date': DateTime(2021, 10, 1), 'cost': 300.0},
{'service_date': DateTime(2023, 1, 1), 'cost': 350.0},
],
);
vehicles.add(car1);
vehicles.add(car2);
vehicles.add(car3);
// Create sample Trucks
Truck truck1 = Truck(
vehicleId: 4,
make: 'Volvo',
modelYear: 2015,
payloadCapacity: 20000,
axles: 2,
deliveries: [
{'1': 'City A'},
{'2': 'City B'},
],
);
Truck truck2 = Truck(
vehicleId: 5,
make: 'Mack',
modelYear: 2018,
payloadCapacity: 15000,
axles: 3,
deliveries: [
{'3': 'City C'},
],
);
Truck truck3 = Truck(
vehicleId: 6,
make: 'Kenworth',
modelYear: 2020,
payloadCapacity: 20000,
axles: 2,
deliveries: [
{'4': 'City D'},
{'5': 'City E'},
],
);
vehicles.add(truck1);
vehicles.add(truck2);
vehicles.add(truck3);
print('Vehicle Details:');
vehicles.forEach((vehicle) => vehicle.displayDetails());
int searchId = 2;
Vehicle? foundVehicle = searchVehicleById(vehicles, searchId);
if (foundVehicle != null) {
print('\nFound Vehicle:');
foundVehicle.displayDetails();
} else {
print('\nVehicle with ID $searchId not found.');
}
List<Car> highMileageCars =
listHighMileageCars(vehicles.whereType<Car>().toList());
print('\nCars with mileage greater than 100,000 km:');
highMileageCars.forEach((car) {
print('ID: ${car.vehicleId}, Make: ${car.make}, Mileage: $
{car.mileage}');
});
double specificPayload = 20000;
List<Truck> sortedTrucks =
trucksByPayloadCapacity(vehicles.whereType<Truck>().toList(),
specificPayload);
print('\nTrucks with payload capacity $specificPayload, sorted by
axles:');
sortedTrucks.forEach((truck) {
print('ID: ${truck.vehicleId}, Make: ${truck.make}, Axles: $
{truck.axles}');
truck.displayDeliveries();
});
}
Output: