UNIVERSITY OF GHANA
Department of Computer Science
Assignment: Object-Oriented Programming in C#
Instructions
• Answer all questions.
• Use Object-Oriented Programming (OOP) principles such as encapsulation, inheritance,
polymorphism, abstraction, and interfaces where appropriate.
• Submit your work PDF containing all C# source files with clear comments explaining
your implementation.
Question 1 – BankAccount Class
Design a class called BankAccount with the following requirements.
Private Fields:
• accountNumber (string)
• accountHolderName (string)
• balance (double)
Public Methods:
• Deposit(double amount)
• Withdraw(double amount)
• GetBalance()
• GetAccountNumber()
Constraints:
• Balance must not be directly accessible outside the class.
• Prevent withdrawals when there are insufficient funds.
• Prevent depositing negative amounts.
• Display appropriate error messages when invalid operations occur.
Tasks:
• Implement the BankAccount class in C#.
• Write a test program demonstrating a valid deposit, an invalid deposit, a valid
withdrawal, and an invalid withdrawal.
Question 2 – Student Class
Create a class called Student with the following fields:
Private Fields:
• studentId
• name
• score
Requirements:
• Provide appropriate getters and setters using properties or methods.
• Ensure the score must be between 0 and 100.
Create a method GetGrade() that returns the student’s grade
according to the following scale:
Score Range Grade
70–100 A
60–69 B
50–59 C
45–49 D
Below 45 F
Tasks:
• Implement the class using proper encapsulation.
• Demonstrate how invalid scores are prevented.
Question 3 – Employee Inheritance
Create a superclass called Employee.
Fields:
• name
• salary
Method:
• CalculateAnnualSalary()
Create two subclasses:
• FullTimeEmployee – includes an additional field bonus and overrides
CalculateAnnualSalary().
• PartTimeEmployee – includes fields hoursWorked and hourlyRate and overrides
CalculateAnnualSalary().
Tasks:
• Implement all classes using inheritance.
• Create objects for both subclasses.
• Demonstrate how annual salary is calculated differently for each employee type.
Question 4 – Vehicle Method Overriding
Create a superclass called Vehicle.
Field:
• brand
Method:
• StartEngine()
Create the following subclasses:
• Car
• Motorcycle
Tasks:
• Override the StartEngine() method in both subclasses.
• Demonstrate method overriding.
• Show the use of the base keyword where appropriate.
Question 5 – Abstract Class and Polymorphism
Create an abstract class called Payment with a method ProcessPayment(double amount).
Create the following subclasses:
• CreditCardPayment
• MobileMoneyPayment
• BankTransferPayment
Tasks:
• Store all payment objects in a List<Payment> or array.
• Use a loop to process the payments.
• Demonstrate runtime polymorphism.
Question 6 – Interface Implementation
Create an interface called IATMOperations with the following methods:
• Withdraw(double amount)
• Deposit(double amount)
• CheckBalance()
Create a class BankATM that implements this interface.
Tasks:
• Implement all interface methods.
• Ensure the system hides internal implementation details.
• Demonstrate ATM operations through a test program.