Uzzal Bhuiyan 21303179 Lab Report-06

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

IUBAT—International University of Business Agriculture and Technology

Department of Computer Science and Engineering

LAB REPORT – 06

Visual Programming Lab - CSC 440

Section – B

Submitted By

Uzzal Bhuiyan

ID: 21303179

Submitted To

Suhala Lamia

Senior Lecturer

Department of Computer Science and Engineering

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.

2. Implement the DisplayMessage Method: Create a method DisplayMessage that matches


the delegate's signature, i.e., it takes a string parameter and prints it to the console.

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);
}

static void Main(string[] args)


{
// Step 3: Create an instance of the delegate and assign it to the DisplayMessage method
PrintMessage print = new PrintMessage(DisplayMessage);

// Step 4: Use the delegate to call the DisplayMessage method


print("Hello, this is a message from the delegate!");

// Optional: Wait for user input to close the console window


Console.ReadLine();
}
}
}

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.

3. Create Delegate Instance: Create an instance of the CalculateCircleArea delegate and


assign it to the GetArea method.

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
}

static void Main(string[] args)


{
// Step 3: Create an instance of the delegate and assign it to the GetArea method
CalculateCircleArea calculateArea = new CalculateCircleArea(GetArea);
// Step 4: Use the delegate to calculate and print the area of a circle with radius 7
double radius = 7;
double area = calculateArea(radius);
Console.WriteLine($"The area of a circle with radius {radius} is: {area}");

// Optional: Wait for user input to close the console window


Console.ReadLine();
}
}
}

Program Output:

Problem-3:
Create two classes, Department and Employee.

• The Employee class should have properties: Id, Name, and Position.

• The Department class should have a List<Employee> property representing the


employees working in that department.

• 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.

3. Create a Method to Display Employees: In the Department class, implement a method


DisplayEmployees() to iterate over the list of employees and display their information.

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; }

public Employee(int id, string name, string position)


{
Id = id;
Name = name;
Position = position;
}
}
}

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
}

// Step 3: Method to display all employees in the department


public void DisplayEmployees()
{
Console.WriteLine("Employees in the department:");

if (Employees.Count == 0)
{
Console.WriteLine("No employees in this department.");
return;
}

foreach (var employee in Employees)


{
Console.WriteLine($"ID: {employee.Id}, Name: {employee.Name}, Position: {employee.Position}"); }
}
}
}

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");

// Create a department and add employees to it


Department department = new Department();
department.Employees.Add(emp1);
department.Employees.Add(emp2);
department.Employees.Add(emp3);

// Display all employees in the department


department.DisplayEmployees();

// Optional: Wait for user input to close the console window


Console.ReadLine();
}
}

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. •

A book is associated with one or more Authors.

• Each author has the properties: Name, Bio, and DateOfBirth.

• 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.

• Create a Library that has multiple Book objects.

• 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:

• A method to display all books and their associated authors.

• A method to calculate the total cost of all books in the collection.

1. Implement the Methods:

• 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; }

public Author(string name, string bio, DateTime dateOfBirth)


{
Name = name;
Bio = bio;
DateOfBirth = dateOfBirth;
}
}
}

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>();
}

// Method to display all books along with their authors


public void DisplayBooksAndAuthors()
{
Console.WriteLine("Books in the Library:");
foreach (var book in Books)
{
Console.WriteLine($"Title: {book.Title}");
Console.WriteLine($"ISBN: {book.ISBN}");
Console.WriteLine($"Genre: {book.Genre}");
Console.WriteLine($"Price: ${book.Price}");
Console.WriteLine("Authors:");
foreach (var author in book.Authors)
{
Console.WriteLine($" Name: {author.Name}, Bio: {author.Bio}, Date of Birth:
{author.DateOfBirth.ToShortDateString()}");
}
Console.WriteLine(new string('-', 20));
}
}
// Method to calculate the total price of all books in the library
public double CalculateTotalPrice()
{
double totalPrice = 0;
foreach (var book in Books)
{
totalPrice += book.Price;
}
return totalPrice;
}
}
}

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));

List<Author> authorsForBook1 = new List<Author> { author1 };


List<Author> authorsForBook2 = new List<Author> { author2 };

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);

// Step 5: Create a Library and add Books


Library library = new Library();
library.Books.Add(book1);
library.Books.Add(book2);

// Display all books and their authors


library.DisplayBooksAndAuthors();

// 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}");

// Optional: Wait for user input to close the console window


Console.ReadLine();
}
}
Program Output:

You might also like