Ass1 App
Ass1 App
Abbottabad Campus
Department of Computer Science
Assignment # 01
Q#1. Implement the following program using dart programming language. (CLO-2, SO 2-4)
a. You are given a list of maps, each containing multiple attributes such as position, name,
and age.
People= [
{"position": 10, "name": "Jawad", "age": 25},
{"position": 33, "name": "Faisal", "age": 30},
{"position": 4, "name": "Zahid", "age": 22},
{"position": 6, "name": "Ali", "age": 29},
{"position": 9, "name": "Noman", "age": 27},
{"position": 4, "name": "Ben", "age": 24},
{"position": 33, "name": "Hassan", "age": 28}
]
Sort the list first with respect to "position". If the positions are the same, sort by "name".
If both "position" and "name" are the same, sort by "age"."
ANSWER
CODE:
void main() {
List<Map<String, dynamic>> students = [
{"position": 10, "name": "Jawad", "age": 25},
{"position": 33, "name": "Faisal", "age": 30},
{"position": 4, "name": "Zahid", "age": 22},
{"position": 6, "name": "Ali", "age": 29},
{"position": 9, "name": "Noman", "age": 27},
{"position": 4, "name": "Ben", "age": 24},
{"position": 33, "name": "Hassan", "age": 28}
];
students.sort((a, b) {
if (a['position'] != b['position']) {
return a['position'].compareTo(b['position']);
} else if (a['name'] != b['name']) {
return a['name'].compareTo(b['name']);
} else {
return a['age'].compareTo(b['age']);
}
});
print(students);
}
OUTPUT:
b. Write a Dart program that creates a list of names and filters this list to create a map.
The map should include only those names whose length is greater than 5. The keys in
the map should be the first letter of each name in uppercase, and the corresponding
values should be the names themselves.
ANSWER
CODE:
void main() {
List<String> names = ["Alice", "Bob", "Catherine", "Daniel", "Eleanor",
"Frank", "Gabriel", "Hannah"];
print(filteredNames);
}
OUTPUT:
c. Initialize a list of Map with the following items:
[{"name":"Ali", "age":45, "marks":32 },
{"name":"Noman", "age":32, "marks":23 },
{"name":"Faisal", "age":41, "marks":43 },
{"name":"Noman", "age":11, "marks":43 },
{"name":"Faisal", "age":8, "marks":43 }]
Print those records whose age is greater than 30 and whose name is either Noman or
Faisal.
ANSWER
CODE:
void main() {
List<Map<String, dynamic>> people = [
{"name": "Ali", "age": 45, "marks": 32},
{"name": "Noman", "age": 32, "marks": 23},
{"name": "Faisal", "age": 41, "marks": 43},
{"name": "Noman", "age": 11, "marks": 43},
{"name": "Faisal", "age": 8, "marks": 43}
];
CODE:
void main() {
List<Map<String, String>> myArray = [
{'name': 'ali', 'age': '45'},
{'name': 'noman', 'age': '34'},
];
OUTPUT:
ANSWER
CODE:
class Person {
int id;
String name;
int age;
void main() {
var students = [
Student(1, "Ali", 20, 3.8, "FA22", "01-09-2022"),
Student(2, "Sara", 22, 3.5, "SP22", "15-02-2022"),
Student(3, "Ahmed", 21, 3.9, "FA21", "01-09-2021")
];
var teachers = [
Teacher(4, "Dr. Khalid", 45, 80000, "Professor", "Computer Science", "10-01-2010"),
Teacher(5, "Ms. Fatima", 38, 60000, "Lecturer", "Mathematics", "15-08-2015"),
Teacher(6, "Mr. Tariq", 50, 70000, "Assistant Professor", "Physics", "20-03-2008")
];
int searchId = 3;
Student? foundStudent;
for (var student in students) {
if (student.id == searchId) {
foundStudent = student;
break;
}
}
Teacher? foundTeacher;
for (var teacher in teachers) {
if (teacher.id == searchId) {
foundTeacher = teacher;
break;
}
}
if (foundStudent != null) {
print("Student found: ${foundStudent.name}");
} else if (foundTeacher != null) {
print("Teacher found: ${foundTeacher.name}");
} else {
print("No person found with ID $searchId");
}
OUTPUT:
f. Given the following list of student objects, write a Dart program that uses the where()
and forEach() methods to filter and print the name, age, and marks of students who
meet the following criteria:
Their age is greater than 30.
Their names are either "Noman" or "Faisal".
Additionally, modify the program to include a count of how many students meet
these criteria and print a summary statement at the end.
ANSWER
CODE:
class Student {
String name;
int age;
int marks;
void main() {
var students = [
Student("Ali", 25, 80),
Student("Noman", 32, 90),
Student("Faisal", 35, 85),
Student("Zara", 28, 75),
Student("Noman", 31, 88),
Student("Faisal", 29, 82)
];
var filteredStudents = students.where((s) => s.age > 30 && (s.name == "Noman" || s.name ==
"Faisal"));
int count = 0;
filteredStudents.forEach((s) {
print("Name: ${s.name}, Age: ${s.age}, Marks: ${s.marks}");
count++;
});
OUTPUT: