0% found this document useful (0 votes)
16 views8 pages

Question No 1

The document discusses four examples demonstrating encapsulation in C# through classes that encapsulate data and expose it through public methods. The examples include a bank account class, a book class, a circle class, and an individual class.

Uploaded by

ishishahid4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Question No 1

The document discusses four examples demonstrating encapsulation in C# through classes that encapsulate data and expose it through public methods. The examples include a bank account class, a book class, a circle class, and an individual class.

Uploaded by

ishishahid4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

QUESTION NO 1:

using System;

namespace encap
{
class Bank
{
private decimal balance;

public void SetDeposit(decimal amount)


{
if (amount > 0)
{
balance += amount;
}
else
{
Console.WriteLine("Please enter a positive value.");
}
}

public decimal GetBalance()


{
Console.WriteLine("Your current balance is: ");
return balance;
}

public void WithdrawBalance(decimal amount)


{
if (amount > 0 && amount <= balance)
{
balance -= amount;
}
else
{
Console.WriteLine("You don't have sufficient balance to withdraw.");
}
}
}

public class Progress


{
static void Main(string[] args)
{
Bank bank = new Bank();

Console.WriteLine("Enter the initial deposit amount:");


decimal initialValue = Convert.ToDecimal(Console.ReadLine());
bank.SetDeposit(initialValue);

Console.WriteLine("Enter the amount to withdraw:");


decimal withdrawAmount = Convert.ToDecimal(Console.ReadLine());
bank.WithdrawBalance(withdrawAmount);

decimal finalBalance = bank.GetBalance();


Console.WriteLine("Final balance after withdrawal: " + finalBalance);
}
}
}

QUESTION NO 2:
using System;

namespace EncapsulationExample
{
class Book
{
private string title;
private string author;
private int numPages;

public void SetTitle(string title)


{
this.title = title;
}

public void SetAuthor(string author)


{
this.author = author;
}

public void SetPageCount(int pageCount)


{
if (pageCount >= 0)
{
Console.WriteLine("Number of pages in your book: " + pageCount);
this.numPages = pageCount;
}
else
{
Console.WriteLine("Number of pages cannot be negative.");
}
}

public string GetAuthor()


{
Console.WriteLine("The author of the book is: ");
return author;
}

public string GetTitle()


{
Console.WriteLine("The title of your book is: ");
return title;
}

public int GetPageCount()


{
return numPages;
}
}

class Program
{
static void Main(string[] args)
{
Book myBook = new Book();

Console.WriteLine("Enter the title of the book:");


string title = Console.ReadLine();
myBook.SetTitle(title);
Console.WriteLine("Enter the author of the book:");
string author = Console.ReadLine();
myBook.SetAuthor(author);

Console.WriteLine("Enter the number of pages in the book:");


int pageCount = int.Parse(Console.ReadLine());
myBook.SetPageCount(pageCount);

Console.WriteLine("Book details:");
Console.WriteLine("Title: " + myBook.GetTitle());
Console.WriteLine("Author: " + myBook.GetAuthor());
Console.WriteLine("Number of Pages: " + myBook.GetPageCount());
}
}
}

QUESTION NO 3:
using System;

public class Circle


{
private double radius;

public void SetRadius(double value)


{
if (value >= 0)
{
this.radius = value;
}
else
{
Console.WriteLine("Please enter a positive value for the radius.");
}
}

public double CalculateArea()


{
Console.WriteLine("The area of the circle is: ");
return 3.141592653589793 * radius * radius;
}

public double CalculateCircumference()


{
Console.WriteLine("The circumference of the circle is: ");
return 2 * 3.141592653589793 * radius;
}
}

public class Program


{
public static void Main(string[] args)
{
Circle myCircle = new Circle();

Console.WriteLine("Enter the radius of the circle:");


double radius = double.Parse(Console.ReadLine());
myCircle.SetRadius(radius);

Console.WriteLine("Area of the circle: " + myCircle.CalculateArea());


Console.WriteLine("Circumference of the circle: " + myCircle.CalculateCircumference());
}
}
QUESTION NO 4:
using System;

public class Individual


{
private string name;
private string gender;
private int age;

public void SetName(string newName)


{
this.name = newName;
}

public string GetName()


{
return name;
}

public void SetGender(string newGender)


{
this.gender = newGender;
}

public string GetGender()


{
return gender;
}

public void SetAge(int newAge)


{
if (newAge >= 0)
{
this.age = newAge;
}
else
{
Console.WriteLine("Age cannot be negative.");
}
}

public int GetAge()


{
return age;
}

public static void Main(string[] args)


{
Individual individual = new Individual();

Console.WriteLine("Enter name:");
individual.SetName(Console.ReadLine());

Console.WriteLine("Enter gender:");
individual.SetGender(Console.ReadLine());

Console.WriteLine("Enter age:");
individual.SetAge(int.Parse(Console.ReadLine()));

Console.WriteLine("Individual details:");
Console.WriteLine("Name: " + individual.GetName());
Console.WriteLine("Gender: " + individual.GetGender());
Console.WriteLine("Age: " + individual.GetAge());

Console.WriteLine("Do you want to change any attribute? (name/gender/age)");


string attribute = Console.ReadLine().ToLower();

switch (attribute)
{
case "name":
Console.WriteLine("Enter new name:");
individual.SetName(Console.ReadLine());
break;
case "gender":
Console.WriteLine("Enter new gender:");
individual.SetGender(Console.ReadLine());
break;
case "age":
Console.WriteLine("Enter new age:");
individual.SetAge(int.Parse(Console.ReadLine()));
break;
default:
Console.WriteLine("Invalid attribute.");
break;
}

Console.WriteLine("Updated Individual details:");


Console.WriteLine("Name: " + individual.GetName());
Console.WriteLine("Gender: " + individual.GetGender());
Console.WriteLine("Age: " + individual.GetAge());
}
}

You might also like