[go: up one dir, main page]

0% found this document useful (0 votes)
17 views11 pages

Lab 8 Submission

The document outlines Lab 8, focusing on Object Oriented Programming (OOP) concepts in Python, including the creation of classes, objects, and methods. It presents various programming exercises such as creating classes for Student, Rectangle, BankAccount, and others, along with their implementations and outputs. The conclusion highlights the learning outcomes of developing and executing basic Python programs using OOP principles.

Uploaded by

2330293
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)
17 views11 pages

Lab 8 Submission

The document outlines Lab 8, focusing on Object Oriented Programming (OOP) concepts in Python, including the creation of classes, objects, and methods. It presents various programming exercises such as creating classes for Student, Rectangle, BankAccount, and others, along with their implementations and outputs. The conclusion highlights the learning outcomes of developing and executing basic Python programs using OOP principles.

Uploaded by

2330293
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/ 11

Experiment Number LAB 8

Date of Experiment 12/03/2025

Date of Submission 20/03/2025

Name of student Arnav Singhal

Roll Number 2330293

Section ECSc-2

• Title of the Experiment:

Object Oriented Programming in Python; Classes, objects, methods & variables

⚫ Aim of the Experiment:


to apply OOP concepts using Python syntax, including the creation of classes,
instantiation of objects, and implementation of methods.

• Programming Language used:


Python
• Problem Statement & Solution:

1. Create a class for a Student with attributes name, age, and grade. Implement a
method to display student information.
SOL:
class Student:
def __init__(self,name,age,grade):
self.name=name
self.age=age
self.grade=grade
def display(self):
print('Hi ',self.name)
print('Your age is: ',self.age)
print('Your grade is: ',self.grade)
Student('Arnav',20,'A').display()

OUTPUT:-

2. Write a Python program to create a Student class with details given in previous
question. Call the method talk() to display student details.

SOL:
class Student:
def __init__(self,name,age,grade):
self.name=name
self.age=age
self.grade=grade
def talk(self):
print('Hi ',self.name)
print('Your age is: ',self.age)
print('Your grade is: ',self.grade)
Student('Arnav',20,'A').talk()

OUTPUT:-

3. Create a class representing a Rectangle with attributes length and width.


Implement methods to calculate its area and perimeter.
SOL:-
class Rectangle:
def __init__(self,length,width):
self.length=length
self.width=width
def area(self):
print('Area of rectangle: ',self.length*self.width)
Rectangle(10,20).area()
print(“Arnav Singhal, 2330293”)

OUTPUT:-

4. Define a class for a BankAccount with methods to deposit, withdraw,


and check balance. Ensure proper encapsulation of data.

SOL:-
class BankAccount:
def __init__(self,account_number,balance):
self.account_number=account_number
self.balance=balance
def deposit(self,amount):
self.balance+=amount
print('Your new balance is: ',self.balance)
def withdraw(self,amount):
if self.balance>=amount:
self.balance-=amount
print('Your new balance is: ',self.balance)
else:
print('Insufficient balance')
def display(self):
print('Your account number is: ',self.account_number)
print('Your balance is: ',self.balance)
BankAccount(123456789,10000).deposit(1000)
Print(“Arnav Singhal 2330293”)

5. Write a Python programme with a class which has attributes like title,actor
and actress in its constructor and another method which prints
all these properties.
(a) Prompt the user to enter all this information and add this info in a
list.
(b) Keep the prompt open add all this information until user reply in
’No’ and then display all this information of the list.
SOL:-
class Movie:
def __init__(self,title,actor,actress):
self.title=title
self.actor=actor
self.actress=actress
def display(self):
print('Title:',self.title)
print('Actor:',self.actor)
print('Actress:',self.actress)
title = input('Enter title: ')
actor = input('Enter actor: ')
actress = input('Enter actress: ')
Movie(title,actor,actress).display()
Print(“Arnav Singhal 2330293”)

OUTPUT:-
6. Implement a class hierarchy for different shapes (e.g., Circle, Square,
Triangle). Include methods to calculate area and perimeter for each
shape.

SOL:-
import math
class Shape:
def area(self): pass
def perimeter(self): pass
class Circle(Shape):
def __init__(self, r): self.r = r
def area(self): return math.pi * self.r**2
def perimeter(self): return 2 * math.pi * self.r
class Square(Shape):
def __init__(self, s): self.s = s
def area(self): return self.s**2
def perimeter(self): return 4 * self.s
class Triangle(Shape):
def __init__(self, a, b, c): self.a, self.b, self.c = a, b, c
def area(self):
s = (self.a + self.b + self.c) / 2
return math.sqrt(s * (s - self.a) * (s - self.b) * (s - self.c))
def perimeter(self): return self.a + self.b + self.c
c = Circle(5)
s = Square(4)
t = Triangle(3, 4, 5)
print("Circle area:", c.area())
print("Circle perimeter:", c.perimeter())
print("Square area:", s.area())
print("Square perimeter:", s.perimeter())
print("Triangle area:", t.area())
print("Triangle perimeter:", t.perimeter())
Print(“Arnav Singhal 2330293”)
OUTPUT:-

7. Develop a class representing a Book with attributes title, author, and


price. Implement methods to set and get book details.

SOL:-
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def details(self):
return f"Title: {self.title}, Author: {self.author}, Price: {self.price}"
book1 = Book("Python Basics", "John Doe", 650)
book1.details()
Print(“Arnav Singhal 2330293”)

OUTPUT:-

8. Design a class hierarchy for different vehicles (e.g., Car, Truck, Motorcycle).
Include methods to calculate mileage and display vehicle information.

SOL:-
class Vehicle:
def __init__(self,model, year):
self.model = model
self.year = year
def info(self):
return f"{self.year} {self.model}"
class Car(Vehicle):
def __init__(self,model, year, doors):
super().__init__(model, year)
self.doors = doors
def info(self):
return f"{super().info()}, {self.doors} doors"
class Truck(Vehicle):
def __init__(self,model, year, capacity):
super().__init__(model, year)
self.capacity = capacity
def info(self):
return f"{super().info()}, {self.capacity} lbs"
car = Car("Toyota", 2022, 4)
truck = Truck("Ford", 2021, 2000)
print(car.info())
print(truck.info())
Print(“Arnav Singhal 2330293”)

OUTPUT:-

9. Define a class for a TodoList with methods to add, remove, and display
tasks. Ensure tasks are stored as objects of another class.

SOL:-
class ToDoList:
def __init__(self):
self.tasks = []

def add_task(self, title):


self.tasks.append(title)
print(f"Task Added: {title}")

def remove_task(self, title):


if title in self.tasks:
self.tasks.remove(title)
print(f"Task Removed: {title}")
else:
print(f"Task not found: {title}")
def display_tasks(self):
if not self.tasks:
print("Your To-Do List is empty.")
else:
print("\nYour To-Do List:")
for idx, task in enumerate(self.tasks, start=1):
print(f"{idx}. {task}")

# Driver code to test the To-Do List functionality


if __name__ == "__main__":
todo_list = ToDoList()

# Adding tasks
todo_list.add_task("Complete Python project")
todo_list.add_task("Read a book")

# Display tasks
todo_list.display_tasks()

# Removing a task
todo_list.remove_task("Read a book")

# Display tasks after removal


todo_list.display_tasks()
print("Arnav Singhal 2330293")
OUTPUT:-

10. Implement a class representing a Bank with methods to add accounts,


remove accounts, and display total balance across all accounts.

SOL:-
class Account:
def __init__(self, num, bal):
self.num = num
self.bal = bal
class Bank:
def __init__(self):
self.accounts = []
def add(self, num, bal):
self.accounts.append(Account(num, bal))
def remove(self, num):
self.accounts = [acc for acc in self.accounts if acc.num != num]
def total(self):
return sum(acc.bal for acc in self.accounts)
b = Bank()
b.add(1, 100)
b.add(2, 200)
print(b.total())
b.remove(1)
print(b.total())

OUTPUT:-

11. Create a class for a Product with attributes name, price, and quantity.
Implement methods to calculate total cost and update quantity.

SOL:
class Product:
def __init__(self, name, price, qty):
self.name = name
self.price = price
self.qty = qty
def total(self):
return self.price * self.qty
def update(self, new_qty):
if new_qty >= 0:
self.qty = new_qty
p = Product("Book", 10, 5)
print(p.total())

p.update(3)
print(p.total())

OUTPUT:

12. Design a class hierarchy for different employees (e.g., Manager, Developer,
Tester). Include methods to calculate salary and display employee details.

SOL:
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def details(self):
return f"{self.name}, {self.salary}"

class Manager(Employee):
def __init__(self, name, salary, bonus):
super().__init__(name, salary)
self.bonus = bonus
def details(self):
return f"{super().details()}, Bonus: {self.bonus}"

class Developer(Employee):
def __init__(self, name, salary, language):
super().__init__(name, salary)
self.language = language
def details(self):
return f"{super().details()}, {self.language}"

m = Manager("Me", 600000, 10000)


d = Developer("Friend", 500000, "Python")

print(m.details())
print(d.details())

OUTPUT:

• Conclusion:

Learned to develop and execute basic Python programs using OOPs

Faculty Signature

You might also like