Mid Lab
Mid Lab
Apply the following concept using Dart programming language. [CLO-2, C3, Marks: 10] a. A class Person
with attributes: ID, name, age. Derive a class from a person named Student. The extra attributes of
Student are CGPA, currently enrolled semester (e.g., FA22 or SP22, etc.), list of maps having Subject
name as Key and Marks as Value. Populate a list with at least 3 records of Student class. Print list of
students whose CGPA is greater than 3.7. Implement a function that calculates the total marks of a
specific student.
void main() {
// Create a list of students
List<Student> students = [
Student(1, "Alice", 21, 3.9, "FA22", {"Math": 85, "CS": 95}),
Student(2, "Bob", 22, 3.5, "SP23", {"Math": 75, "CS": 85}),
Student(3, "Charlie", 20, 3.8, "FA23", {"Math": 88, "CS": 92}),
];
// Student class
class Student {
int id;
String name;
int age;
double cgpa;
String semester;
Map<String, int> subjects;
Student(
this.id, this.name, this.age, this.cgpa, this.semester,
this.subjects);
int calculateTotalMarks() {
return subjects.values.reduce((sum, marks) => sum + marks);
}
}
2. Create a StoreItem class with itemID, name, and price attributes. Define a derived Electronics class
with additional warrantyPeriod and category attributes, and populate a list with at least 4 electronics
records using generative constructors. In the main() function, write a function that filters and returns
only items priced above $1000 using an anonymous function. Use spread operators to add more items
to the electronics list, including items from a list of map objects. Implement a switch statement to
categorize items by warranty period, printing "Short Warranty" for items under 12 months and "Long
Warranty" for items with 12 months or more.
class Vehicle {
int id;
String type;
String brand;
// Constructor
Vehicle(this.id, this.type, this.brand);
}
// Constructor
Car(int id, String type, String brand, {this.model, this.year,
this.price})
: super(id, type, brand);
}
void main() {
// Initializing a list of Car objects using positional and optional
named parameters
List<Car> cars = [
Car(1, 'Sedan', 'Toyota', model: 'Camry', year: 2019, price: 25000),
Car(2, 'SUV', 'Honda', model: 'CR-V', year: 2021, price: 30000),
Car(3, 'Hatchback', 'Ford', model: 'Focus', year: 2017, price:
18000),
Car(4, 'Sedan', 'BMW', model: '3 Series', year: 2020, price: 35000),
Car(5, 'Truck', 'Ford', model: 'F-150', year: 2015, price: 40000),
];
3. A software development company is creating a financial tracking application using Dart to help small
business owners manage their expenses, income, and budget. The application needs to be efficient in
handling variable data, such as daily income, different categories of expenses, and monthly budgets. The
data can change frequently, and there may be instances where some information (like future budget
predictions) is not yet available. Question: 1) Explain how you would design the application using Dart's
data types, especially focusing on when to use final, const, late, and nullable types for handling financial
data. 2) Discuss how you would implement variables for handling different categories of expenses and
explain the scenarios in which you would use lists, sets, and maps in this application. 3) Describe how
using Dart’s ?? operator and late keyword can help manage situations where some data (like projected
income) might not be immediately available at runtime.
class StoreItem {
int itemID;
String name;
double price;
void main() {
// List of Electronics items
List<Electronics> electronics = [
Electronics(101, 'Smartphone', 1200.0, 24, 'Mobile'),
Electronics(102, 'Laptop', 1500.0, 18, 'Computing'),
Electronics(103, 'Headphones', 200.0, 6, 'Audio'),
Electronics(104, 'Smartwatch', 250.0, 12, 'Wearable'),
];
// Adding more items using spread operator and a list of map objects
List<Map<String, dynamic>> moreItems = [
{
'itemID': 105,
'name': 'Tablet',
'price': 800.0,
'warrantyPeriod': 12,
'category': 'Mobile'
},
{
'itemID': 106,
'name': 'Camera',
'price': 1300.0,
'warrantyPeriod': 24,
'category': 'Photography'
},
];