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 = [Link]((fruit) => '${[Link]()}$
{[Link](1)} is delicious');
[Link]((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 [Link];
}
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:');
[Link]((element) {
[Link]((key, value) {
print('$key: $value');
});
});
print('\nValues Only:');
[Link]((element) {
print([Link]);
});
}
Output:
Question no8:
void main() {
var myArray1 = [3, 4, 5];
var myArray2 = [6, 7, 8];
[Link](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([Link], [Link], [Link]);
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, [Link], [Link],
[Link])
: super(id, name, age);
double averagePerformanceScore() {
if ([Link]) return 0.0;
double totalScore = [Link](0.0, (sum, project) => sum
+ project['performance_score']);
return totalScore / [Link];
}
}
class Manager extends Employee {
double salary;
String position;
String department;
List<Map<String, String>> teamMembers;
Manager(int id, String name, int age, [Link], [Link],
[Link], [Link])
: super(id, name, age);
void displayTeamMembers() {
print('Team members of Manager $name in department $department:');
[Link]((member) {
print('ID: ${[Link]}, Name: ${[Link]}');
});
}
}
Employee? searchById(List<Employee> employees, int id) {
return [Link]((employee) => [Link] == id);
}
List<Staff> listHighPerformingStaff(List<Staff> staffList) {
return [Link]((staff) => [Link]() >
4.5).toList();
}
List<Manager> managersInDepartment(List<Manager> managers, String
department) {
return managers
.where((manager) => [Link] == department)
.toList()
..sort((a, b) => [Link]([Link]));
}
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},
]);
[Link](staff1);
[Link](staff2);
[Link](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'},
]);
[Link](manager1);
[Link](manager2);
// Display details of all employees
print('Employee Details:');
[Link]((employee) => [Link]());
// Search for an employee by ID
int searchId = 2;
Employee? foundEmployee = searchById(employees, searchId);
if (foundEmployee != null) {
print('\nFound Employee:');
[Link]();
} else {
print('\nEmployee with ID $searchId not found.');
}
// List staff members with a performance score greater than 4.5
List<Staff> highPerformingStaff =
listHighPerformingStaff([Link]<Staff>().toList());
print('\nHigh Performing Staff:');
[Link]((staff) {
print('${[Link]} with average performance score: $
{[Link]()}');
});
// Display managers in a specific department sorted by salary
String departmentToCheck = 'IT';
List<Manager> itManagers =
managersInDepartment([Link]<Manager>().toList(),
departmentToCheck);
print('\nManagers in $departmentToCheck department (sorted by
salary):');
[Link]((manager) {
print('${[Link]} - Salary: \$${[Link]}');
[Link]();
});
}
Output:
Question no 11:
class Vehicle {
int vehicleId;
String make;
int modelYear;
Vehicle({
required [Link],
required [Link],
required [Link],
});
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 [Link],
required [Link],
required [Link],
}) : super(vehicleId: vehicleId, make: make, modelYear: modelYear);
double totalServiceCost() {
return [Link](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 [Link],
required [Link],
required [Link],
}) : super(vehicleId: vehicleId, make: make, modelYear: modelYear);
void displayDeliveries() {
print('Deliveries for Truck ID: $vehicleId');
[Link]((delivery) {
[Link]((deliveryId, destination) {
print('Delivery ID: $deliveryId, Destination: $destination');
});
});
}
}
Vehicle? searchVehicleById(List<Vehicle> vehicles, int vehicleId) {
return [Link]((vehicle) => [Link] ==
vehicleId);
}
List<Car> listHighMileageCars(List<Car> cars) {
return [Link]((car) => [Link] > 100000).toList();
}
List<Truck> trucksByPayloadCapacity(List<Truck> trucks, double
payloadCapacity) {
return trucks
.where((truck) => [Link] == payloadCapacity)
.toList()
..sort((a, b) => [Link]([Link]));
}
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},
],
);
[Link](car1);
[Link](car2);
[Link](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'},
],
);
[Link](truck1);
[Link](truck2);
[Link](truck3);
print('Vehicle Details:');
[Link]((vehicle) => [Link]());
int searchId = 2;
Vehicle? foundVehicle = searchVehicleById(vehicles, searchId);
if (foundVehicle != null) {
print('\nFound Vehicle:');
[Link]();
} else {
print('\nVehicle with ID $searchId not found.');
}
List<Car> highMileageCars =
listHighMileageCars([Link]<Car>().toList());
print('\nCars with mileage greater than 100,000 km:');
[Link]((car) {
print('ID: ${[Link]}, Make: ${[Link]}, Mileage: $
{[Link]}');
});
double specificPayload = 20000;
List<Truck> sortedTrucks =
trucksByPayloadCapacity([Link]<Truck>().toList(),
specificPayload);
print('\nTrucks with payload capacity $specificPayload, sorted by
axles:');
[Link]((truck) {
print('ID: ${[Link]}, Make: ${[Link]}, Axles: $
{[Link]}');
[Link]();
});
}
Output: