OOP in C# Tasks
OOP in C# Tasks
Lab Manual 2
Introduction
After a week of rigorous coding, Welcome back!
You have learned all about the C# in the previous lab manuals.
Let's move on to the next, new, and interesting concepts.
Learning Objectives:
1. Package the related data as single unit (Class)
2. Create variables (Object) to use the data.
3. Explain Role of the Constructors (Copy Constructors) and Memory
Representation of the objects.
4. Behavior of the class (Identify and write the functions in side the class)
Scanior 1
Your code is creating a simple class Student with some fields like Name, EcatMarks, FscMarks,
MatricMarks, and Aggregate. In your Main method, you create an instance of the Student class
called student, set some values, and then create another instance called student2 and assign the
values of student to it. After that, you change the Name of student2 and print both student.Name
and student2.Name to observe the impact on the original object.
Here's the key point to understand: When you assign one object to another in C# (e.g., student2 =
student), you are not creating a new object. Instead, both variables (student and student2) now
refer to the same object in memory. Therefore, any changes made through one variable will
affect the other.
In your example, when you change the Name of student2, it also changes the Name of the
original student because they both point to the same object in memory. This is due to the
reference assignment (student2 = student).
So, if you print both student.Name and student2.Name after changing student2.Name, you will
see that both names are now "Sarmad" because they both refer to the same Student object.
It's important to be aware of this behavior when working with object assignments in C#. If you
want to create a truly independent copy of an object, you would need to implement a copy
constructor or another method to explicitly duplicate the object's values.
Explanation:
The default constructor (public Student()) allows the creation of Student objects without
specifying initial values. The attributes will be initialized to their default values (e.g., null for
string, 0 for float).
The copy constructor (public Student(Student student)) takes another Student object as a
parameter and copies its values to create a new Student object with the same attributes. This is
useful when you want to create a new instance of a Student with the same attributes as an
existing one.
Driver Code
Self Assessment Task 2: Imagine you are tasked with building a basic calculator class in
C#. The class should allow users to perform arithmetic operations such as addition,
subtraction, multiplication, and division. Make sure you create these functions inside the
class and you have to initialize the class with the constructor.
Important Note: You have to complete this task with one class only and write the driver code to
test your functionality.
Self Assessment Task 3: Imagine you are developing a simulation of an Automated Teller
Machine (ATM) in C#. Your task is to create an ATM class that models the functionality of a
typical ATM. The class should support the following operations:
Important Note: You have to complete this task with one class only and write the driver code to
test your functionality.
1. Initialization:
○ Create an ATM class.
○ Include initializes the ATM with a specified balance.
2. Deposit:
○ Implement a method named deposit that takes an amount as an argument and adds
it to the ATM's balance.
3. Withdrawal:
○ Implement a method named withdraw that takes an amount as an argument and
deducts it from the ATM's balance. Ensure that the withdrawal amount does not
exceed the available balance.
4. Check Balance:
○ Implement a method named check_balance that returns the current balance of the
ATM.
5. Transaction History:
○ Implement a method named transaction_history that maintains a record of all
transactions made on the ATM. The transaction history should include details
such as the type of transaction (deposit or withdrawal) and the amount involved.
For this you can make an array/list of strings and everytime any transaction
performs you have to add that string to the array or list.
You may take a two minute break, as there is much more code to
come.
Challenge # 1:
Student Management System with Class and Behavior
Identify and write the suitable functions (Behavior of your class) in your code.
Task: Write a Menu driven program that shows the following four menu options
1. Add Student.
2. Show Students.
3. Calculate Aggregate
4. Top Students.
● Add Student allows users to add a Student’s information that includes Student
Name, matric marks, fsc marks, ecat marks.
○ Hint: Take input from the user in simple variables and then Use the
parameterized constructor to create the object.
● Show Students displays all the added students on the screen.
● Calculate Aggregate will calculate the aggregate of all the available students.
● Top Students lists the information of the top 3 students.
Challenge # 2:
Products Management System with Class
Task: Identify and write the suitable functions (Behavior of your class) in your code.
Write a program that shows three menu options
1. Add Products.
2. Show Products.
3. Total Store Worth.
● Add Product allows the user to add product information that includes ID, Name,
price, Category, BrandName, Country.
● Show Product displays all the added products on the screen.
● Total Store Worth calculates the sum of the price of all the products.
Challenge # 3:
Task. Identify and write the suitable functions (Behavior of your class) in your code.
Convert the signUp/signIn application that you developed in the previous lab by using the
class concepts.
Make a class named MUser with 3 attributes namely
● Username
● Password
● Role
The data should be loaded from the file and loaded into the attributes of the class.