Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main() {
- // Create a list containing three objects of each class.
- List<People> peopleList = [
- People(name: 'Alice', age: 25),
- People(name: 'Bob', age: 32),
- People(name: 'Charlie', age: 28),
- Worker(name: 'David', age: 40, post: 'Engineer', salary: 60000),
- Worker(name: 'Eve', age: 35, post: 'Manager', salary: 75000),
- Worker(name: 'Fred', age: 29, post: 'Analyst', salary: 55000),
- Student(name: 'Grace', age: 20, marks: [85, 90, 92], subjects: ['Math', 'Science', 'History']),
- Student(name: 'Henry', age: 22, marks: [78, 82, 88], subjects: ['English', 'Art', 'Music']),
- Student(name: 'Ivy', age: 21, marks: [92, 95, 98], subjects: ['Physics', 'Chemistry', 'Biology']),
- ];
- // Print information about each person using the toString method.
- print('Information about each person:');
- for (var person in peopleList) {
- print(person);
- }
- // Print the names of those who are younger than 30 years old.
- print('\nNames of people younger than 30:');
- for (var person in peopleList) {
- if (person.age < 30) {
- print(person.name);
- }
- }
- // Calculate the average salary of all workers.
- double averageSalary = 0;
- int workerCount = 0;
- for (var person in peopleList) {
- if (person is Worker) {
- averageSalary += person.salary;
- workerCount++;
- }
- }
- averageSalary = workerCount > 0 ? averageSalary / workerCount : 0;
- // Count the number of Worker objects with a salary below the average.
- int workersBelowAverage = 0;
- for (var person in peopleList) {
- if (person is Worker && person.salary < averageSalary) {
- workersBelowAverage++;
- }
- }
- print('\nNumber of workers with salary below average: $workersBelowAverage');
- // Print information about the subjects of each Student.
- print('\nSubjects of each student:');
- for (var person in peopleList) {
- if (person is Student) {
- print('${person.name}: ${person.subjects}');
- }
- }
- }
- class People {
- final String name;
- final int age;
- People({required this.name, required this.age});
- @override
- String toString() => 'Name: $name, Age: $age';
- }
- final class Worker extends People {
- final String post;
- final int salary;
- Worker({
- required super.name,
- required super.age,
- required this.post,
- required this.salary,
- });
- @override
- String toString() => 'Name: $name, Age: $age, Post: $post, Salary: $salary';
- }
- final class Student extends People {
- final List<int> marks;
- final List<String> subjects;
- Student({
- required super.name,
- required super.age,
- required this.marks,
- required this.subjects,
- });
- @override
- String toString() => 'Name: $name, Age: $age, Marks: $marks, Subjects: $subjects';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement