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

Lab 5

dart programming

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)
12 views9 pages

Lab 5

dart programming

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
Lab 5 Tasks

Course: Mobile Application Development Class: BSE-6C


Name: Safa Noor Marks: 20 marks

Q#1. Write the code of the following programs in Dart language.

1. Write an example of function definition and function call with named parameters.

Code:

void test({required String name, int age = 0, String? city}) {


print("Hello, my name is $name.");
print("I am $age years old.");
if (city != null) {
print("I live in $city.");
} else {
print("City is not specified.");
}
}

void main() {
test(name: "Safa", age: 25, city: "Karachi");
test(name: "Ali", city: "Lahore");
test(name: "Aisha", age: 30);
}

Output:
2. Write arrow functions for the following equations:
2 4
A=a +b
2
Z= p +5 t+ A

Code:

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() {

double a = 2;
double b = 3;
double p = 4;
double t = 1;

double A = calculateA(a, b);


double Z = calculateZ(p, t, A);

print("A: $A");
print("Z: $Z");
}

Output:
3. Write arrow functions for the following equations:
2
A=x +2 xy + p . Z
Z = a 2 + 4.B2 – 8b + 2a
2
B=n + qn+1

Code:

double calB(double n, double q) => (n * n) + (q * n) + 1;


double calZ(double a, double b, double B) => (a * a) + (4 * B * B) - (8
* b) + (2 * a);
double calA(double x, double y, double p, double Z) => (x * x) + (2 * x
* y) + (p * Z);

void main() {

double x = 3;
double y = 2;
double p = 1.5;
double a = 2;
double b = 4;
double n = 5;
double q = 1;

double B = calB(n, q);


double Z = calZ(a, b, B);
double A = calA(x, y, p, Z);

print("B: $B");
print("Z: $Z");
print("A: $A");
}
Output:

4. Suppose the equation is:


Z = x2 + 4y2 – 8N2
Where N is represented by a separate equation:
N = p2 + q2
Solve ‘Z’ with an arrow function, such that you need to define the arrow function N
within the body of Z.

Code:

double calculateZ(double x, double y, double p, double q) {


double calculateN() => (p * p) + (q * q);
return (x * x) + (4 * y * y) - (8 * (calculateN() * calculateN()));
}

void main() {

double x = 3;
double y = 2;
double p = 1;
double q = 2;

double Z = calculateZ(x, y, p, q);


print("Z: $Z");
}

Output:
5. Given the following list: ['apples', 'bananas', 'oranges'];
Append a string with each element of the list and capitalize each element of list. Use a
combination of map and forEach function.

Code:

void main() {

List<String> home = ['Kitchen', 'room', 'lawn'];


String appendString = ' is pretty';

var modifiedhome = home.map((item) {


return '${item.toUpperCase()}$appendString'; }).toList();

modifiedhome.forEach((item) {
print(item);
});
}

Output:

6. Create a small calculator application using typedef functions performing these


operations, add, subtract, multiply, and divide.
Code:

typedef Calculator = double Function(double a, double b);

void main(){

Calculator add = (double a, double b) => a + b;


Calculator subtract = (double a, double b) => a - b;
Calculator multiply = (double a, double b) => a * b;
Calculator divide = (double a, double b) => a / b;

Map <String,Calculator> operations ={


'add':add, 'subtract':subtract, 'mul':multiply, 'divide':divide
};
print('Add:');
print(operations['add']!(10,5));
print('Subtract:');
print(operations['subtract']!(10,5));
print('Multiply:');
print(operations['mul']!(10,5));
print('Divide:');
print(operations['divide']!(10,5));
}

Output:

7. 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
Code:

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) {
element.values.forEach((value) {
print(value);
});
});
}

Output:
8. Suppose we have the following arrays:
var myArray1 = [3, 4, 5]
var myArray2 = [6, 7, 8]
Write code to append the myArray2 into myArray1.
Code:

void main() {
var myArray1 = [3, 4, 5];
var myArray2 = [6, 7, 8];
var combinedArray = [...myArray1, ...myArray2];

print(combinedArray);
}

Output:

9. Write an example of defining an arrow function within another arrow function.


Code:

void main() {
var outerFunction = (int x) => (int y) {
var innerFunction = (int z) => x + y + z;
return innerFunction;
};

var innerFunc = outerFunction(5)(10);


int result = innerFunc(10);
print("Result: $result");
}

Output:

You might also like