You are on page 1of 4

DCIT 318: PROGAMMING II

ASSIGNMENT 2
(SUBMIT ANSWER IN MS WORD OR PDF DOCUMENT)

ID: 10882603

Q1. Bank Account

Create a BankAccount class with private attributes accountNumber and balance. Implement
public methods Deposit() and Withdraw() to modify the balance. Use appropriate access
modifiers to encapsulate the attributes and ensure proper data manipulation.

 Solution

using System;

class BankAccount
{
private string accountNumber;
private decimal balance;

public BankAccount(string accountNumber, decimal initialBalance)


{
this.accountNumber = accountNumber;
this.balance = initialBalance;
}

public void Deposit(decimal amount)


{
if (amount > 0)
{
balance += amount;
Console.WriteLine($"Deposited GHC{amount}. New balance: GHC{balance}");
}
else
{
Console.WriteLine("Invalid amount for deposit.");
}
}

public void Withdraw(decimal amount)


{
if (amount > 0 && amount <= balance)
{
balance -= amount;
Console.WriteLine($"Withdrawn GHC{amount}. New balance: GHC{balance}");
}
else
{
Console.WriteLine("Insufficient funds or invalid amount for withdrawal.");
}
}
}

Q2. Employee

Construct an Employee class with private attributes name, designation, and salary. Implement
public properties to manipulate these attributes. Utilize access modifiers to enforce
encapsulation and control access to the class members.

 Solution

class Employee
{
private string name;
private string designation;
private decimal salary;

public string Name


{
get { return name; }
set { name = value; }
}

public string Designation


{
get { return designation; }
set { designation = value; }
}

public decimal Salary


{
get { return salary; }
set { salary = value; }
}
}

Q3. Student

Design a Student class with private attributes name, age, and grade. Include public methods
to set and retrieve these attributes. Use appropriate access modifiers to encapsulate the
attributes and control their accessibility.

Solution

class Student
{
private string name;
private int age;
private int grade;

public string name {


get {return value; }
set {name = value; }
}
public int age {
get {return age; }
set {age = value; }
}
public int grade {
get {return grade; }
set {grade = value; }
}
}

Q4. Car

Develop a Car class with private attributes make, model, and year. Implement public
properties to access these attributes. Ensure appropriate access modifiers to provide
controlled access to the class members.

 Solution

class Car
{
private string make;
private string model;
private int year;

public string Make


{
get { return make; }
set { make = value; }
}

public string Model


{
get { return model; }
set { model = value; }
}

public int Year


{
get { return year; }
set { year = value; }
}
}
Q5. Library Book

Create a class called LibraryBook with private attributes title (string), author (string), and
available (bool). Implement public methods to get and set the availability of the book, as well
as to retrieve the book's title and author. Use appropriate access modifiers to encapsulate the
data properly.

Solution

class LibraryBook
{
private string title;
private string author;
private bool available;

public string Title


{
get { return title; }
set { title = value; }
}

public string Author


{
get { return author; }
set { author = value; }
}

public bool IsAvailable()


{
return available;
}

public void SetAvailability(bool status)


{
available = status;
}
}

You might also like