0% found this document useful (0 votes)
6 views9 pages

Ass1 App

mobile application development

Uploaded by

safa noor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Ass1 App

mobile application development

Uploaded by

safa noor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COMSATS University Islamabad

Abbottabad Campus
Department of Computer Science
Assignment # 01

Course: Mobile Application Development Class: BSE - 6C


Instructor: Muhammad Rafay Hannan Marks: 20 marks

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.

Input= ["Alice", "Bob", "Catherine", "Daniel", "Eleanor", "Frank", "Gabriel",


"Hannah"]

Output= {C: Catherine, D: Daniel, E: Eleanor, G: Gabriel, H: Hannah}

ANSWER

CODE:

void main() {
List<String> names = ["Alice", "Bob", "Catherine", "Daniel", "Eleanor",
"Frank", "Gabriel", "Hannah"];

Map<String, String> filteredNames = {


for (var name in names)
if (name.length > 5) name[0].toUpperCase(): name
};

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}
];

for (var person in people) {


if (person['age'] > 30 && (person['name'] == "Noman" || person['name'] == "Faisal")) {
print(person);
}
}
}
OUTPUT:

d. Suppose you have the following array,


List<Map<String, String>> myArray = [
{'name': 'ali', 'age': '45'},
{'name': 'noman', 'age': '34'},
];
Display the key and value of array elements.
Display the values of the array
ANSWER

CODE:

void main() {
List<Map<String, String>> myArray = [
{'name': 'ali', 'age': '45'},
{'name': 'noman', 'age': '34'},
];

for (var item in myArray) {


item.forEach((key, value) {
print('$key: $value');
});
}

for (var item in myArray) {


print(item.values);
}
}

OUTPUT:

e. Create a class Person with attributes: id, name, age.


Derive two classes from a person, named Student and Teacher.
The extra attributes of Student are CGPA, currently enrolled semester (e.g., FA22 or
SP22, etc.), admission date.
The extra attributes of Teacher are salary, designation (Lecturer, Assistant Professor,
Professor, etc.), department, and joining date.
Populate a list of at least 3 records in each class using class objects.
A user should be able to search for a student or teacher with the provided ID. You
should
store objects of Teacher and Student in a list.
Print list of students whose CGPA is greater than 3.7.

ANSWER

CODE:

class Person {
int id;
String name;
int age;

Person(this.id, this.name, this.age);


}

class Student extends Person {


double cgpa;
String semester;
String admissionDate;

Student(int id, String name, int age, this.cgpa, this.semester, this.admissionDate)


: super(id, name, age);
}

class Teacher extends Person {


double salary;
String designation;
String department;
String joiningDate;

Teacher(int id, String name, int age, this.salary, this.designation, this.department,


this.joiningDate)
: super(id, name, 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");
}

print("\nStudents with CGPA > 3.7:");


for (var student in students) {
if (student.cgpa > 3.7) {
print("Name: ${student.name}, CGPA: ${student.cgpa}");
}
}
}

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;

Student(this.name, this.age, this.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++;
});

print("\nTotal students meeting the criteria: $count");


}

OUTPUT:

You might also like