Csharp Lab Task
Csharp Lab Task
ASSIGNMENT 4
INSTRUCTIONS: Answer one (1) question from each section by writing appropriate C# code.
ENCAPSULATION
QUESTION 1.
You are required to create a CommissionEmployee class in C# to represent an employee who earns
compensation based on a percentage of their total sales. The class must enforce encapsulation
principles by restricting direct access to sensitive attributes.
Class
CommissionEmployee
Class Attributes
Methods
Constructor – Initializes all attributes . Also prevent the (default) no argument constructor from being
called
Implementation Tasks
A. Implement the CommissionEmployee class in C# with proper encapsulation.
B. Create an instance of the CommissionEmployee class.
C. Update the employee’s grossSales and commissionRate, then display the updated details.
D.. Calculate and display the employee’s earnings using the earnings() method.
QUESTION 2.
You are required to implement a Library Management System in C# using encapsulation principles. The
system should manage books and library members, allowing members to borrow and return books while
tracking book availability.
Class 1
Book Class
Class Attributes
book_id (string): Unique identifier for the book.
title (string): Title of the book.
author (string): Author of the book.
available_copies (int): Number of available copies .
Methods
Class Attributes
member_id (string): Unique identifier for the member.
name (string): Name of the member.
borrowed_book (Book or Null): Tracks the borrowed book .
-
Methods
Constructor – Initializes member_id and name.
borrow_book(Book book) – Allows borrowing if the member has no book and the book is
available.
Implementation Tasks
Class 1
Patient Class
Class Attributes:
Methods
Getters and Setters – Retrieve and update attributes while ensuring data integrity.
Class 2
Doctor Class
Class Attributes
doctor_id (string): Unique identifier for the doctor.
name (string): Name of the doctor.
specialization (string): Doctor’s field of specialization.
Methods
Getters and Setters – Retrieve and update attributes with proper validation.
Implementation Tasks
You need to design an Airline Reservation System in C# using encapsulation to securely manage flight
details, passenger information, and reservation operations..
Class 1
Flight Class
Class Attributes:
flight_number (string): Unique identifier for the flight.
Methods
Getters and Setters – Retrieve and update attribute values with validation.
Class Attributes:
Methods
Implementation Tasks
QUESTION 5
Design a Banking System in C# using encapsulation principles. The system should securely manage
customer accounts, transactions, and financial analytics.
Class 1
BankAccount Class
Class Attributes:
account_number (String): The account number of the bank account.
account_holder (String): The name of the account holder.
balance (float): The current balance in the account (must be ≥ 0.0).
interest_rate (float): The annual interest rate for the account (must be between 0.0 and
1.0).
Methods
Constructor: Initializes all attributes while ensuring valid balance and interest rate.
Getters and Setters: Retrieve and updates attributes
deposit(float amount): Adds the specified amount to the balance.
withdraw(flaot amount): Deducts the specified amount from the balance (if sufficient funds are
available).
calculateInterest(): Returns the annual interest earned using the formula: balance *
interest_rate.
Implementation Tasks
A. Implement the BankAccount class with encapsulation in c#.
B. Create an instance of the BankAccount class.
C. Update the account balance and display updated details
D. Calculate and display annual interest earned.
INHERITANCE
QUESTION 1
Extend a CommissionEmployee class into a subclass called BasePlusCommissionEmployee. The system
should manage employees who earn based on commission, and those who have a base salary in
addition to commissions.
Base Class
CommissionEmployee
Class Attributes
first_name (String): The employee’s first name.
last_name (String): The employee’s last name.
social_security_number (String): A unique identifier for the employee.
gross_sales (float): The employee’s total sales amount.
commission_rate (float): The commission percentage (between 0 and 1).
Methods
Constructor: Initializes all attributes.
earnings(): Returns commission earnings (gross_sales * commission_rate).
displayEmployeeDetails(): Prints employee details and their earnings.
Derived Class
BasePlusCommission
Class Attributes
Inherits all fields from CommissionEmployee.
base_salary (float): A guaranteed base salary for the employee.
Methods
Constructor: Calls the superclass constructor and initializes inherited attributes and base_salary.
earnings(): Returns total earnings as base_salary + (gross_sales *
commission_rate).
set_base_salary(float new_salary): Updates the base salary with validation (must be ≥ 0).
Implementation Taaks
A. Create an instance of Commission-Only Employees in C#
B. Create an instance of BasePlusCommission Salary
C. Calculate and Display Earnings on each employee
D. Update baseSalary for a BasePlusCommissionEmployee instance and print the update
earnings.
QUESTION 2
You are tasked with developing a Vehicle Rental Management System using inheritance in C#. The
system should manage different types of vehicles.
Base Class
Vehicle
Class Attributes
vehicle_id (String): Unique vehicle identifier.
brand (String): Brand name of the vehicle.
model (String): Model name.
is_available (Bool): Vehicle availability status (default: True).
Methods
Constructor: Initializes all attributes and sets is_available = True.
rent_vehicle(): If available, marks the vehicle as rented; otherwise, displays an error message.
return_vehicle(): Marks the vehicle as available and confirms the return.
Derived Class
Car
Class Attributes
seating_capacity (int): Number of seats in the car.
Constructor
Initializes all inherited attributes and seating_capacity.
Methods
calculate_rental_cost(int days): Calculates rental cost using the formula 1000 * days
+ seating_capacity * 50. Prints "Rental cost for <days> days: <calculated amount>"
Implementation Tasks
A. Create a Car Instance
B. Rent and return a vehicle
C. Calculate Rental Cost
QUESTION 3
You are tasked with designing an E-Commerce System to manage different types of users and orders
using inheritance principles.
Base Class
User
Class Attributes
user_id (String): Unique identifier for the user.
name (String): User’s full name.
Methods
Constructor: Initializes user_id and name.
print_user_details(): Displays the user’s ID and name.
Derived Class
Customer - Extends User Class
Class Attributes
email: str → Customer’s email address.
cart_item1: str | Null → First item added to the cart.
cart_item2: str | Null → Second item added to the cart.
Methods
Constructor: Calls the parent constructor and initializes
email,cart_item1 and cart_item2 as None.
add_item_to_cart(String item) : Adds an item to the cart.
ViewCart(): Displays cart items if they exist.
Derived Class
Order - Extends Customer Class
Class Attributes
orderId: String → Unique identifier for the order.
orderItem1: String | null → First ordered item.
orderItem2: String | null → Second ordered item.
Methods
Constructor: Calls the parent constructor and initializes orderId.
PlaceOrder(): Transfers cart items to order details and clears the cart.
PrintOrderDetails(): Displays the order ID, customer details, and ordered items.
Implementation Tasks
A. Create Users:
Customer("C001", "Alice", "[email protected]")
Customer("C002", "Bob", "[email protected]")
B.Customers Add Items to Cart:
Alice: "Laptop", "Mouse"
Bob: "Smartphone", "Headphones"
C. Place Orders:
Alice places an order with her cart items.
Bob places an order with his cart items.
QUESTION 4
You are tasked with designing a Hospital Management System to manage different types of staff and
their roles using inheritance principles.
Base Class
Staff
Class Attributes
staffId (String): Unique identifier for each staff member.
name (String): Name of the staff member.
department (String): Department where the staff member works.
Constructor
Initializes staffId, name, and department.
Methods
displayDetails(): Prints "Staff ID: <staff_id>, Name: <name>,
Department: <department>"
Derived Class
Doctor - Inherits Staff, representing a doctor with a specialization.
Class Attributes
Inherits staffId, name, and department from Staff.
specialization (String): Doctor’s area of expertise.
years_of_experience (int): Number of years the doctor has practiced.
Constructor
Calls the superclass constructor to initialize inherited attributes.
Initializes specialization and years_of_experience
Methods
displayDetails(): Prints
"Doctor ID: <staff_id>, Name: <name>, Department: <department>, Specialization:
<specialization>, Experience: <years_of_experience> years"
Derived Class
Nurse - Inherits Staff, representing a nurse with shift details..
Class Attributes
Inherits staffId, name, and department from Staff.
shift (String): Assigned shift (e.g., "Day", "Night").
patients_assigned (int): Number of patients under care
Constructor
Calls the superclass constructor to initialize inherited attributes.
Initializes shift and patients_assigned
Methods
displayDetails(): Prints
"Nurse ID: <staff_id>, Name: <name>, Department: <department>, Shift: <shift>, Patients
Assigned: <patients_assigned>"
Independent Class
HospitalManagementSystem
Methods
register_doctor(Doctor doctor): Calls display_details() on the Doctor instance.
Implementation Tasks
Base Class
Employee - Represents a general restaurant employee.
Class Attributes
employee_id (String): Unique identifier for the employee.
name (String): Name of the employee.
Constructor
Initializes employee_id and name
Methods
display_details(): Prints "Employee ID: <employee_id>, Name: <name>"
Derive Class
Chef - Represents a chef with additional details.
Class Attributes
Inherits employee_id and name from Employee.
specialty (String): Type of cuisine the chef specializes in.
Constructor
Calls the superclass constructor to initialize inherited attributes.
Initializes specialty.
Methods
display_details(): Prints
"Chef ID: <employee_id>, Name: <name>, Specialty: <specialty>"
prepare_dishes(): Prints
"Chef <name> is preparing <specialty> dishes."
Derive Class
Waiter - Represents a Waiter with additional details.
Class Attributes
Inherits employee_id and name from Employee.
assigned_section (String): Section of the restaurant assigned to the waiter.
Constructor
Calls the superclass constructor to initialize inherited attributes.
Initializes assigned_section
Methods
display_details(): Prints
"Waiter ID: <employee_id>, Name: <name>, Section: <assigned_section>"
serve_customers(): Prints
"Waiter <name> is serving customers in the <assigned_section> section."
Independent Class
RestaurantManagementSystem - Handles employee tasks.
Methods
assign_chef_task(Chef chef): Calls prepare_dishes() on the Chef instance.
Implementation Tasks
A. Create Employees:
Chef("E001", "Alice", "Italian")
Waiter("E002", "Bob", "Outdoor")
B. Assign Tasks:
Call assign_chef_task() for the chef.
Call assign_waiter_task() for the waiter.
POLYMORPHISM
QUESTION 1
You are tasked with designing a Transportation Management System to handle various types of vehicles
and their operations using polymorphism. The system must demonstrate both runtime polymorphism
(method overriding) and compile-time polymorphism (method overloading).
Abstract Class
Vehicle
Class Attributes
Constructor
Methods
refuel(float liters): Adds fuel and prints the updated fuel level.
calculate_range(): Abstract method to be implemented in subclasses.
Derive Class
Car - Extends Vehicle
Class Attributes
Constructor
Initializes fuel_efficiency.
Methods
Independent Class
TransportationManager
Method
operate_vehicle(Vehicle vehicle): Calls calculate_range() on any
Vehicle instance, demonstrating runtime polymorphism.
Implementation Tasks
A. Create Vehicles:
Car("C001", "Sedan", 50, 15)
B. Refuel Sedan using refuel()
C. Use operate_Vehicle() to process all
QUESTION 2
You are tasked with designing a Banking System that demonstrates both compile-time (method
overloading) and run-time polymorphism (method overriding). The system should handle different
types of accounts and operations.
Base Class
BankAccount
Class Attributes
Constructor
Methods
deposit(float amount, String note): Overloaded method that also prints the
transaction note.
withdraw(float amount): Decreases balance if funds are available, else prints an error.
Derive Class
savingsAccount - Extends Bank Account
Class Attributes
Constructor
Intializes interest_Rate
Methods
Implementation Tasks
A. Create Accounts:
SavingsAccount("Alice", "SA123", 500, 3%)
QUESTION 3
You are tasked with designing an E-Commerce System to handle various types of products and dynamic
pricing using polymorphism.
Abstract Class
Product
Class Attributes
Constructor
Methods
Derive Class
Electronics
Class Attributes
warranty_period (int): Warranty duration in months.
Constructor
Intializes warranty_period
Methods
Derive Class
Clothing
Class Attributes
size (String): Size of the clothing item.
Constructor
Methods
Derive Class
Cart
Methods
Implementation Tasks
A. Create Products
Electronics("E001", "Laptop", 1000.0, 24)
Clothing("C001", "Winter Jacket", 200.0, "M", 20.0)
QUESTION 4
You are tasked with designing a Staff Management System for an organization. The system must
demonstrate polymorphism to calculate staff Annual salary
Abstract Class
StaffMember
Class Attributes
Constructor
Initializes all attributes
Methods
Derive Class
Staff
Constructor
Methods
Implementation Tasks
A. Create a staff Member
B. Display Staff Details
C. Calculate total Annual Salary
QUESTION 5
You are tasked with designing a Church Management System that demonstrates method overriding
(runtime polymorphism) and method overloading (compile-time polymorphism).
Abstract Class
StaffMember
Class Attributes
Constructor
Initializes name and member_Id.
Methods
get_Contribution(): Returns 0.0 (default for general members).
give_Offering( double amount): Prints "Offering given: <amount>".
Derive Class
Pastor - Inherits ChurchMember
Class Attributes
tithe (double): Monthly tithe contribution.
Constructor
Initializes name, member_Id, and tithe
Methods
Override get_Contribution(): Returns tithe.
Overload give_Offering(double amount, String message): Prints "Offering
given: <amount>. Note: <message>".
Implementation Tasks
A. Create Staff Members
StaffMember("John Doe", "M001") for a general memnber
Pastor("Rev. Smith", "P001", 500.0)
C. Give Offerings:
General member calls give_offering(100.0).
Pastor calls give_offering(200.0, "For the church renovation").
ABSTRACTION
QUESTION 1
You are tasked with implementing abstraction in a payroll system using C#. The system
should define an abstract Employee class as a base for different types of employees.
Abstract Class
Employee
Constructor
Initializes name and employee_id.
Methods
Getter methods: Provide access to name and employee_id.
Abstract method calculate_pay(): Must be implemented by subclasses.
Derive Class
FullTimeEmployee - Extends Employee
Encapsulated Class Attributes
salary (float): The full-time employee’s salary.
Constructor
Initializes name, employee_id, and salary.
Methods
get_salary(): Returns salary
Implement calculate_pay() to return: "FullTimeEmployee Pay: <salary>".
Implementation Tasks
A. Create the abstract Employee class with the specified attributes and methods in C#
B. Implement the FullTimeEmployee subclass and define calculate_pay().
C. Instantiate a FullTimeEmployee object.
D. Display the employee’s details using the getter methods.
QUESTION 2
You are tasked with designing a Medical Record Management System using C#. The
system should enforce abstraction by defining a base class for different types of medical
personnel while allowing specific roles and specializations to vary.
Abstract Class
MedicalPersonnel
Methods
Getter methods: Provide access to name and id.
perform_duties(): Defines the duties of medical personnel (must be implemented in subclasses).
get_specialization(): Defines the specialization of the personnel (must be implemented in
subclasses).
display_details(): Prints the name and ID of the personnel.
Derive Class
Doctor - Extends MedicalPersonnel
Constructor
Initializes name, id, and specialization.
Methods
Implement perform_duties() to return:
"Doctor <name>: Diagnoses patients, prescribes medication, and conducts surgeries."
Derive Class
Nurse - Extends MedicalPersonnel
Encapsulated Class Attributes
department (String): The department the nurse works in (e.g., ICU, Emergency).
Constructor
Initializes name, id, and department.
Methods
Implement perform_duties() to return:
"Nurse <name>: Provides patient care, administers medications, and assists doctors."
Implement get_specialization() to return department
Implementation Tasks
A. Create the MedicalPersonnel abstract class with the required methods.
B. Implement the Doctor subclass, ensuring they define perform_duties() and
get_specialization().
C. Write statements to:
Create a list of at least one Doctor, one Nurse.
Use a loop to:
Abstract Class
Device
Class Attributes
device_id (String): Unique identifier.
brand (String): Device brand.
model (String): Device model.
Constructor
Initializes device_id, brand, and model
Abstract Methods
calculate_power_consumption(): Computes power consumption in kWh.
calculate_maintenance_cost(): Computes yearly maintenance cost.
getDetails(): Returns device details.
Derive Class
Laptop - Extends Device
Class Attributes
processor_power (float): Processor power in watts.
Methods
Override calculate_power_consumption(): (processorPower *
dailyUsageHours * 365) / 1000
Implementation Tasks
A. Create a Laptop instance
B. Display Device details.
C. Display Power consumption.
D. Display Maintenance cost.
QUESTION 4
You are tasked with creating a 2D Shape Management System for a design application that allows users
to manage, resize, and render various 2D shapes. Use abstraction to define the core operations for all
shapes and provide specific implementations for different shape types.
Interface
shape2D
Methods:
draw(): Displays the shape's details.
resize(float factor): Scales the shape by a given factor.
move(float delta_x, float delta_y): Adjusts the shape's position
Class
Rectangle - Implements 2D interface
Class Attributes
Constructor:
Initializes all attributes.
Methods:
draw(): Prints the rectangle’s color, position, width, and height.
resize(float factor): Scales width and height by factor.
move(float delta_x, float delta_y): Updates position based on the given deltas.
Class
shapeManager
Class Atrributes
Shape(Shape2D): Stores a Shape2D object.
Methods
add_shape(Shape2D shape): Adds a new shape to the manager.
Implementation Tasks
A. Create Shapes:
Rectangle: Red, (2,3), 5,10.
QUESTION 5
Interface
Department
Attributes
dept_name(String)→ Name of the department.
dept_head(String) → Head of the department.
Methods:
print_department_details(): Displays department details.
Class
Student - Implements department
Class Atrributes
student_name(String)→ Student’s full name.
Methods
set_student_details(): Inputs and assigns student details, including
department and hostel.
get_student_details(): return all student information.
Class
UniversityManager
Class Atrributes
student_record(Student) → Stores a single student's information at a time.
Methods
admit_student(Student student): Assigns the given student as the current
record.
Implementation Tasks
A. Define Interface for Departments
B. Create a Student class that implements Department.
C. Create a UniversityManager class to handle student-related operations.
D. Admit a new student by providing all necessary details.
E. Migrate a student by updating their hostel details.
F. Display student details using displayStudentDetails()