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

OOPs

The document contains several Python class implementations demonstrating object-oriented programming concepts. It includes classes for Rectangle, Person, Circle, BankAccount, Student, Book, and Car, each with methods for specific functionalities such as calculating area, managing bank transactions, and displaying details. Each class is instantiated with example data and demonstrates basic operations like input handling and method calls.

Uploaded by

Dibya Shankar
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)
5 views4 pages

OOPs

The document contains several Python class implementations demonstrating object-oriented programming concepts. It includes classes for Rectangle, Person, Circle, BankAccount, Student, Book, and Car, each with methods for specific functionalities such as calculating area, managing bank transactions, and displaying details. Each class is instantiated with example data and demonstrates basic operations like input handling and method calls.

Uploaded by

Dibya Shankar
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

Q1

class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width

def area(self):
return self.length * self.width

def perimeter(self):
return 2 * (self.length + self.width)

def display(self):
print(f"Rectangle:")
print(f" Length: {self.length}")
print(f" Width: {self.width}")
print(f" Area: {self.area()}")
print(f" Perimeter: {self.perimeter()}")

a=int(input("Enter the length:"))


b=int(input("Enter the breadth:"))
rect = Rectangle(a, b)
rect.display()
Q2
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def set_name(self, name):


self.name = name

def get_name(self):
return self.name

def set_age(self, age):


self.age = age

def get_age(self):
return self.age

def greet(self):
print(f"Hello, my name is {self.name}!")

person = Person("Deepika", 38)

person.set_name("Sanskruti")

print("Name:", person.get_name())

person.set_age(18)

print("Age:", person.get_age())

person.greet()
Q3
import math

class Circle:
def __init__(self, radius):
self.radius = radius

def area(self):
return math.pi * (self.radius ** 2)

def circumference(self):
return 2 * math.pi * self.radius

def display(self):
print("Circle:")
print(" Radius:", self.radius)
print(" Area:", self.area())
print(" Circumference:", self.circumference())

r = int(input("Enter the radius:"))


circle = Circle(r)

circle.display()
Q4
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance

def deposit(self, amount):


if amount > 0:
self.balance += amount
print(f"Deposited: INR{amount:.2f}")
else:
print("Deposit amount must be positive.")

def withdraw(self, amount):


if amount > self.balance:
print("Insufficient funds.")
elif amount <= 0:
print("Withdrawal amount must be positive.")
else:
self.balance -= amount
print(f"Withdrew: ${amount:.2f}")

def check_balance(self):
return self.balance

def display_account(self):
print("Account Details:")
print(" Account Number:", self.account_number)
print(" Balance: INR{:.2f}".format(self.balance))

account = BankAccount("12345678", 1000)


dep=int(input("Enter the amt to be deposited:"))
account.deposit(dep)
wit =int(input("Enter the amt to be withdrawed:"))
account.withdraw(wit)

print("Balance: INR{:.2f}".format(account.check_balance()))

account.display_account()
Q5
class Student:
def __init__(self, student_id, name):
self.student_id = student_id
self.name = name
self.grades = []

def add_grade(self, grade):


if 0 <= grade <= 100:
self.grades.append(grade)
else:
print("Grade must be between 0 and 100.")

def calculate_average(self):
if self.grades:
return sum(self.grades) / len(self.grades)
else:
return 0

def display_student(self):
print("Student Information:")
print(" Student ID:", self.student_id)
print(" Name:", self.name)
print(" Grades:", self.grades)
print(" Average Grade: {:.2f}".format(self.calculate_average()))

student = Student("S12345", "Peter Parker")

student.add_grade(90)
student.add_grade(95)
student.add_grade(92)

student.display_student()
Q6
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages

def display_details(self):
print("Book Details:")
print(" Title:", self.title)
print(" Author:", self.author)
print(" Pages:", self.pages)

def is_long(self):
return self.pages > 300

bk = Book("Wings Of Fire", "Dr. A.P.J. Abdul Kalam", 196)

bk.display_details()

if bk.is_long():
print("The book is a long book.")
else:
print("The book is not a long book.")
Q7
from datetime import datetime

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def display_details(self):
print("Car Details:")
print(" Make:", self.make)
print(" Model:", self.model)
print(" Year:", self.year)

def age(self):
current_year = datetime.now().year
return current_year - self.year

c = Car("Ford", "Mustang", 1969)

c.display_details()

print("Car Age:", c.age(), "years")

You might also like