Answer 01

    class Rectangle {</span></span>
<span id="cb1-2" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">        double length;
        double width;
        
        // Constructor to initialize the properties
        Rectangle(this.length, this.width);
        
        // Method to calculate area
        double area() {</span></span>
<span id="cb1-10" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            return length * width;
        }
        
        // Method to calculate perimeter
        double perimeter() {</span></span>
<span id="cb1-15" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            return 2 * (length + width);
        }
    }
    
    void main() {</span></span>
<span id="cb1-20" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">        // Creating an instance of Rectangle with length 5 and width 3
        Rectangle rect = Rectangle(5.0, 3.0);
        
        // Printing the area and perimeter
        print('Area: ${rect.area()}');
        print('Perimeter: ${rect.perimeter()}');
    }

Answer 02

    class Student {</span></span>
<span id="cb2-2" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">        String name;
        int age;
        List<int> grades;
        
        // Constructor to initialize the properties
        Student(this.name, this.age, this.grades);
        
        // Method to calculate the average grade
        double averageGrade() {</span></span>
<span id="cb2-11" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            int total = grades.reduce((a, b) => a + b);
            return total / grades.length;
        }
    }
    
    void main() {</span></span>
<span id="cb2-17" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">        // Creating an instance of Student
        Student student = Student('John Doe', 20, [90, 85, 88, 92]);
        
        // Printing the student's details
        print('Name: ${student.name}');
        print('Age: ${student.age}');
        print('Average Grade: ${student.averageGrade()}');
    }

Answer 03

        class BankAccount {</span></span>
<span id="cb3-2" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            String accountNumber;
            double balance;
            
            // Constructor to initialize account number and balance
            BankAccount(this.accountNumber, this.balance);
            
            // Method to deposit money
            void deposit(double amount) {</span></span>
<span id="cb3-10" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                balance += amount;
            }
            
            // Method to withdraw money
            void withdraw(double amount) {</span></span>
<span id="cb3-15" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                if (amount <= balance) {</span></span>
<span id="cb3-16" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                    balance -= amount;
                } else {</span></span>
<span id="cb3-18" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                    print('Insufficient funds.');
                }
            }
        }
        
        void main() {</span></span>
<span id="cb3-24" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            // Creating an instance of BankAccount
            BankAccount account = BankAccount('123456789', 500.0);
            
            // Making a deposit of 200
            account.deposit(200.0);
            
            // Making a withdrawal of 100
            account.withdraw(100.0);
            
            // Printing the final balance
            print('Final Balance: \$${account.balance}');
        }

Answer 04

        class Book {</span></span>
<span id="cb4-2" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            String title;
            String author;
            String isbn;
            
            // Constructor to initialize the properties
            Book(this.title, this.author, this.isbn);
            
            // Display book information
            void displayBook() {</span></span>
<span id="cb4-11" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                print('Title: $title, Author: $author, ISBN: $isbn');
            }
        }
        
        class Library {</span></span>
<span id="cb4-16" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            List<Book> books = [];
            
            // Add a book to the library
            void addBook(Book book) {</span></span>
<span id="cb4-20" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                books.add(book);
            }
            
            // Remove a book from the library based on ISBN
            void removeBook(String isbn) {</span></span>
<span id="cb4-25" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                books.removeWhere((book) => book.isbn == isbn);
            }
            
            // Find a book by title
            Book? findBookByTitle(String title) {</span></span>
<span id="cb4-30" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                for (var book in books) {</span></span>
<span id="cb4-31" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                    if (book.title == title) {</span></span>
<span id="cb4-32" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                        return book;
                    }
                }
                return null;
            }
            
            // List all books in the library
            void listBooks() {</span></span>
<span id="cb4-40" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                if (books.isEmpty) {</span></span>
<span id="cb4-41" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                    print('No books in the library.');
                } else {</span></span>
<span id="cb4-43" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                    for (var book in books) {</span></span>
<span id="cb4-44" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                        book.displayBook();
                    }
                }
            }
        }
        
        void main() {</span></span>
<span id="cb4-51" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">            // Create a library
            Library library = Library();
            
            // Add books to the library
            library.addBook(Book('1984', 'George Orwell', '1234567890'));
            library.addBook(Book('To Kill a Mockingbird', 'Harper Lee', '0987654321'));
            library.addBook(Book('The Great Gatsby', 'F. Scott Fitzgerald', '1122334455'));
            
            // List all books in the library
            print('Listing all books:');
            library.listBooks();
            
            // Find a book by title
            print('\nSearching for "1984":');
            Book? book = library.findBookByTitle('1984');
            if (book != null) {</span></span>
<span id="cb4-67" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                book.displayBook();
            } else {</span></span>
<span id="cb4-69" style="{color: inherit; text-decoration: inherit; display: inline-block; line-height: 1.25} :empty{height:1.2em};">                print('Book not found.');
            }
            
            // Remove a book by ISBN
            print('\nRemoving "To Kill a Mockingbird":');
            library.removeBook('0987654321');
            
            // List all books again
            print('\nListing all books after removal:');
            library.listBooks();
        }
Last modified: Thursday, 14 November 2024, 10:03 AM