0% found this document useful (0 votes)
11 views4 pages

Ex 09

The document outlines a Python programming exercise focused on object-oriented programming concepts, including creating a BankAccount class for banking operations, implementing multiple inheritance with a Manager class, method overriding in Student classes for different educational levels, and demonstrating composition with a Student class containing an Address object. Each section includes class definitions, methods, and user input handling to illustrate the concepts. The exercises aim to reinforce understanding of OOP principles in Python.

Uploaded by

Caleb.R
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)
11 views4 pages

Ex 09

The document outlines a Python programming exercise focused on object-oriented programming concepts, including creating a BankAccount class for banking operations, implementing multiple inheritance with a Manager class, method overriding in Student classes for different educational levels, and demonstrating composition with a Student class containing an Address object. Each section includes class definitions, methods, and user input handling to illustrate the concepts. The exercises aim to reinforce understanding of OOP principles in Python.

Uploaded by

Caleb.R
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/ 4

9 - Python Programming using Oops Concepts

Exp - 9(a) - To write a Python program that simulates basic banking operations using object-
oriented programming concepts. The program demonstrates the use of instance methods, class
methods, and static methods within a BankAccount class, along with user input handling and
validation.
class BankAccount:
bank_name = "National Bank"

def __init__(self, account_holder, account_number, balance=0.0):


self.account_holder = account_holder
self.account_number = account_number
self.balance = balance

def deposit(self, amount):


self.balance += amount
print("Deposited ₹" + str(amount) + ". New balance: ₹" + str(self.balance))

def withdraw(self, amount):


if amount > self.balance:
print("Insufficient funds.")
else:
self.balance -= amount
print("Withdrew ₹" + str(amount) + ". New balance: ₹" + str(self.balance))

def show_balance(self):
print(self.account_holder + "'s balance: ₹" + str(self.balance))

@classmethod
def change_bank_name(cls, new_name):
cls.bank_name = new_name
print("Bank name changed to: " + cls.bank_name)

@staticmethod
def is_valid_account_number(account_number):
return isinstance(account_number, str) and len(account_number) == 6 and
account_number.isdigit()

# ---- Main Program with User Input ----

print("Welcome to " + BankAccount.bank_name)

name = input("Enter account holder name: ")


acc_number = input("Enter 6-digit account number: ")

# Validate account number using static method


if not BankAccount.is_valid_account_number(acc_number):
print("Invalid account number format! Must be 6 digits.")
else:
initial_balance = float(input("Enter initial balance: ₹"))

# Create account object


account = BankAccount(name, acc_number, initial_balance)

# Perform some actions


deposit_amount = float(input("Enter amount to deposit: ₹"))
account.deposit(deposit_amount)

withdraw_amount = float(input("Enter amount to withdraw: ₹"))


account.withdraw(withdraw_amount)

account.show_balance()

# Optional: change bank name


change = input("Do you want to change the bank name? (yes/no): ").lower()
if change == "yes":
new_name = input("Enter new bank name: ")
BankAccount.change_bank_name(new_name)
Exp - 9(b) - To implement and demonstrate the concept of multiple inheritance in Python by
creating a Manager class that inherits features from both Person and Employee base classes. The
program collects user input and displays combined personal, employee, and managerial details
using inherited and custom methods.
# First base class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def display_personal_info(self):
print("Name: " + self.name)
print("Age: " + str(self.age))

# Second base class


class Employee:
def __init__(self, employee_id, department):
self.employee_id = employee_id
self.department = department

def display_employee_info(self):
print("Employee ID: " + self.employee_id)
print("Department: " + self.department)

# Derived class with multiple inheritance


class Manager(Person, Employee):
def __init__(self, name, age, employee_id, department, team_size):
# Call both base class constructors
Person.__init__(self, name, age)
Employee.__init__(self, employee_id, department)
self.team_size = team_size

def display_manager_info(self):
print("Team Size: " + str(self.team_size))

# ---- Main Program ----

# User input
name = input("Enter manager's name: ")
age = int(input("Enter age: "))
emp_id = input("Enter employee ID: ")
dept = input("Enter department: ")
team_size = int(input("Enter number of team members: "))
# Create Manager object
mgr = Manager(name, age, emp_id, dept, team_size)

# Display all information


print("\n--- Manager Information ---")
mgr.display_personal_info()
mgr.display_employee_info()
mgr.display_manager_info()

Exp - 9(c) - To implement method overriding in Python by creating a base Student class and
derived classes Undergraduate and Postgraduate that override the calculate_marks method to
perform different calculations such as percentage and grade evaluation based on student type.
# Base class
class Student:
def calculate_marks(self, marks):
print("Total marks (base):", sum(marks))

# Derived class for undergraduate students


class Undergraduate(Student):
# Override to calculate percentage
def calculate_marks(self, marks):
total = sum(marks)
percentage = (total / (len(marks) * 100)) * 100 # assuming each subject max 100
print("Undergraduate Total Marks:", total)
print("Undergraduate Percentage: {:.2f}%".format(percentage))

# Derived class for postgraduate students


class Postgraduate(Student):
# Override to calculate grade based on average
def calculate_marks(self, marks):
total = sum(marks)
avg = total / len(marks)
if avg >= 85:
grade = "A"
elif avg >= 70:
grade = "B"
elif avg >= 50:
grade = "C"
else:
grade = "F"
print("Postgraduate Total Marks:", total)
print("Postgraduate Grade:", grade)

# Main program
marks = [85, 90, 78, 92]

print("=== Undergraduate Student ===")


ug = Undergraduate()
ug.calculate_marks(marks)

print("\n=== Postgraduate Student ===")


pg = Postgraduate()
pg.calculate_marks(marks)
Exp - 9(d) - To demonstrate the concept of composition in Python by creating a Student class that
contains an Address object, illustrating the “has-a” relationship between objects and how one
class can use another class as its attribute.
# Class representing an Address
class Address:
def __init__(self, city, state, zipcode):
self.city = city
self.state = state
self.zipcode = zipcode

def show_address(self):
print("Address: {}, {}, {}".format(self.city, self.state, self.zipcode))

# Class representing a Student, which has an Address object


class Student:
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address # Composition: Student HAS an Address

def show_details(self):
print("Name:", self.name)
print("Age:", self.age)
self.address.show_address() # Delegation to Address object

# ---- Main program ----


addr = Address("TN", "Coimbatore", "611010")
student = Student("Sanjana", 20, addr)

student.show_details()

You might also like