Uzzal Bhuiyan 21303179 Lab Report-06
Uzzal Bhuiyan 21303179 Lab Report-06
Uzzal Bhuiyan 21303179 Lab Report-06
LAB REPORT – 06
Section – B
Submitted By
Uzzal Bhuiyan
ID: 21303179
Submitted To
Suhala Lamia
Senior Lecturer
Date: 08/12/2024
Problem-1:
Write a simple C# program that defines a delegate called PrintMessage which takes a string
parameter and returns void. Then, implement a method DisplayMessage that prints a given
message to the console. Create an instance of the delegate and use it to call the
DisplayMessage method.
Objective:
The objective of this program is to demonstrate how to define a delegate, implement a method
that matches the delegate signature, and then use the delegate to call the method.
Algorithm:
1. Define a Delegate: Define a delegate named PrintMessage which accepts a string
parameter and returns void.
3. Create a Delegate Instance: Instantiate the delegate and associate it with the
DisplayMessage method.
4. Call the Method Using the Delegate: Use the delegate instance to invoke the
DisplayMessage method and display the message.
Program:
using System;
namespace DelegateExample
{
// Step 1: Define the delegate with a string parameter and void return type
public delegate void PrintMessage(string message);
class Program
{
// Step 2: Implement the method that matches the delegate's signature
static void DisplayMessage(string message)
{
Console.WriteLine(message);
}
Program Output:
Problem-2:
Write a delegate CalculateCircleArea that takes a double parameter (radius) and returns a
double (the area of the circle). Write a method GetArea that calculates the area of a circle using
the formula π * radius^2. Create an instance of the delegate and use it to calculate and print
the area for a circle with a radius of 7.
Objective:
The objective of this program is to define a delegate CalculateCircleArea, which calculates the
area of a circle based on a radius, and then use this delegate to calculate and print the area of a
circle with a given radius.
Algorithm:
1. Define the Delegate: Define a delegate CalculateCircleArea that accepts a double
parameter (the radius) and returns a double (the area of the circle).
2. Implement the GetArea Method: Create a method GetArea that takes the radius as an
argument and returns the area of the circle using the formula π * radius^2.
4. Invoke the Delegate: Use the delegate to calculate the area of a circle with a radius of 7
and print the result.
Program:
using System;
namespace CircleAreaExample
{
// Step 1: Define the delegate that takes a double (radius) and returns a double (area)
public delegate double CalculateCircleArea(double radius);
class Program
{
// Step 2: Implement the method to calculate the area of the circle
static double GetArea(double radius)
{
return Math.PI * Math.Pow(radius, 2); // Area formula: π * radius^2
}
Program Output:
Problem-3:
Create two classes, Department and Employee.
• The Employee class should have properties: Id, Name, and Position.
• Write a method in the Department class that displays all employees in the department.
Objective:
The objective of this program is to create two classes, Department and Employee. The
Employee class will store information about employees, and the Department class will manage
a list of employees and provide a method to display all employees in the department.
Algorithm:
1. Define the Employee Class: Create a class Employee with properties Id, Name, and
Position to store information about an employee.
2. Define the Department Class: Create a class Department that contains a List<Employee>
to represent the employees working in that department.
4. Create Instances and Test the Program: Instantiate employees and add them to a
department. Then, call the method to display the list of employees.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp21
{
// Step 1: Define the Employee class with Id, Name, and Position
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Position { get; set; }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp21
{
// Step 2: Define the Department class with a List of Employees
public class Department
{
public List<Employee> Employees { get; set; }
public Department()
{
Employees = new List<Employee>(); // Initialize the list of employees
}
if (Employees.Count == 0)
{
Console.WriteLine("No employees in this department.");
return;
}
using ConsoleApp21;
class Program
{
static void Main(string[] args)
{
// Step 4: Create instances of Employee and Department
Employee emp1 = new Employee(1, "John Doe", "Software Developer");
Employee emp2 = new Employee(2, "Jane Smith", "Project Manager");
Employee emp3 = new Employee(3, "Sam Johnson", "UX Designer");
Program Output:
Problem-4:
Imagine you're developing for a local library. In this system, you need to model various entities
such as Library, Book, and Author.
• Each book has the following properties: Title, ISBN, Genre, and Price. •
• The library has a collection of books. Each book is part of the library's collection.
• The library also needs to have a method to display all the books along with their
authors.
• Additionally, the library should have a method to calculate the total cost of all books in
its collection.
• Each Book can have one or more Author objects associated with it.
• Implement a method in the Library class to display all books along with their authors.
• Implement a method in the Library class to calculate the total price of all the books in
the library.
Objective:
The goal of this program is to model a library system that contains books, authors, and methods
to display information about the books and calculate the total cost of the books in the library's
collection.
Algorithm:
1. Define the Author Class: Create a class Author with properties such as Name, Bio, and
DateOfBirth to represent the authors of the books.
2. Define the Book Class: Create a class Book with properties like Title, ISBN, Genre, and
Price. A book can be associated with one or more authors, so we will use a list of Author
objects.
3. Define the Library Class: Create a class Library that holds a collection of Book objects.
This class will have:
• The DisplayBooksAndAuthors method will display each book and its associated authors.
• The CalculateTotalPrice method will sum up the prices of all books in the collection.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp21
{
// Step 1: Define the Author class with properties: Name, Bio, and DateOfBirth
public class Author
{
public string Name { get; set; }
public string Bio { get; set; }
public DateTime DateOfBirth { get; set; }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp21
{
// Step 2: Define the Book class with properties: Title, ISBN, Genre, Price, and a list of Authors
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
public string Genre { get; set; }
public double Price { get; set; }
public List<Author> Authors { get; set; }
public Book(string title, string isbn, string genre, double price, List<Author> authors) {
Title = title;
ISBN = isbn;
Genre = genre;
Price = price;
Authors = authors;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp21
{
// Step 3: Define the Library class with a collection of Books and methods to display books and calculate total
price
public class Library
{
public List<Book> Books { get; set; }
public Library()
{
Books = new List<Book>();
}
using ConsoleApp21;
class Program
{
static void Main(string[] args)
{
// Step 4: Create instances of Author and Book
Author author1 = new Author("J.K. Rowling", "British author, best known for the Harry Potter series.", new
DateTime(1965, 7, 31));
Author author2 = new Author("George Orwell", "English novelist, known for 1984 and Animal Farm.", new
DateTime(1903, 6, 25));
Book book1 = new Book("Harry Potter and the Philosopher's Stone", "978-0747532699", "Fantasy", 19.99,
authorsForBook1);
Book book2 = new Book("1984", "978-0451524935", "Dystopian", 9.99, authorsForBook2);
// Calculate and display the total price of all books in the library
double totalPrice = library.CalculateTotalPrice();
Console.WriteLine($"Total price of all books in the library: ${totalPrice}");