Ex No3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Ex.

No:3
Date:
OBJECT ORIENTED PROGRAMMING 
AIM:
To Write a Python Programs to implement Object Oriented Programming Paradigm

3a. Write a program to create bank account and to perform deposit and withdraw
operations using class and objects

ALGORITHM:
 Start the program
 Create class named Bank Account
 Initialize the constructor to make the balance zero
 Define and implement the withdraw operation.
 Define and implement the deposit operation.
 Create  the  object
 Call the withdraw and deposit function using object
 Stop

PROGRAM:
class BankAccount:
def __init__(self):
self.balance = 0
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
a = BankAccount()
b = BankAccount()
print(a.deposit(100))
print(b.deposit(50))
print(b.withdraw(10))
print(a.withdraw(10))

OUTPUT :
100
50
40
90

3b.Write a program to create employee class using constructor and destructor and  to get ID, name,
gender , city and salary
ALGORITHM:
 Start the program
 Initialize all the values using constructor.
 Initialize the destructor
 Get the input from user.
 Display the data
 Create the object for the employee class
 Call functions using class
 Stop

PROGRAM :
class Employee:
def __init__(self): #Constructor
self.__id = 0
self.__name = ""
self.__gender = ""
self.__city = ""
self.__salary = 0
print("Object Initialized.")
def __del__(self): #Destructor
print("Object Destroyed.")
def setData(self):
self.__id=int(input("Enter Id\t:"))
self.__name = input("Enter Name\t:")
self.__gender = input("Enter Gender:")
self.__city = input("Enter City\t:")
self.__salary = int(input("Enter Salary:"))
def __str__(self):
data = "["+str(self.__id)
+","+self.__name+","+self.__gender+","+self.__city+","+str(self.__salary)+"]"
return data
def showData(self):
print("Id\t\t:",self.__id)
print("Name\t:", self.__name)
print("Gender\t:", self.__gender)
print("City\t:", self.__city)
print("Salary\t:", self.__salary)

def main():
#Employee Object
emp=Employee()
emp.setData()
emp.showData()
print(emp)

if __name__=="__main__":
main()

OUTPUT :
Object Initialized.
Enter Id        :101
Enter Name      :Pankaj
Enter Gender:Male
Enter City      :Delhi
Enter Salary:70000
Id              : 101
Name    : Pankaj
Gender  : Male
City    : Delhi
Salary  : 70000
[101,Pankaj,Male,Delhi,70000]
Object Destroyed.

3c.To create the student class that consists of name, id and age attribute and to create the object
of the student, to print attribute name of the object, to reset the value of the age, to
print the modified value of age  , to print true if the student contains the attribute with name and 
to delete the attribute age.             

ALGORITHM :
 Start the program
 Create the student class with name , id and age.
 Create the object of the student class.
 Print attribute name of the object.
 Reset the value of attribute age to 23  
 Prints the modified value of age  
 Delete the attribute’s age.
 Stop

PROGRAM :
class Student:
def __init__(self, name, id, age):
self.name = name
self.id = id
self.age = age
# creates the object of the class Student
s = Student("John", 101, 22)
# prints the attribute name of the objects
print(getattr(s, 'name'))
# reset the value of attribute age to 23
setattr(s, "age", 23)
# prints the modified value of age
print(getattr(s, 'age'))
print(hasattr(s, 'id'))
# deletes the attribute age
delattr(s, 'age')

OUTPUT  :
John
23
True
AttributeError: 'Student' object has no attribute 'age'

3d.To implement the object oriented concepts.There are 258 computers available in computer
programming lab where each computers are used eight hours per day. Write a Python  program
using classes and objects that contain getDetail() for getting input from 
user,calculatesecondperDay() for calculating the usage of each computer in seconds per day,
calculateminutesperWeek() for calculating the usage of each computer in minutes per
week ,calculatehourperMonth() for calculating usage of each computer in hour per month and
calculatedayperYear() for calculating usage of each computer in day per yearList all the
Components of structured programming language.

ALGORITHM  :
 Start the program
 Create the  calc class with getdetail() to get hours.
 Define calcultesecondsperday() function  to calculate seconds peer day.
 Define calculateminutesperweek() function to calculate minutes in a week.
 Create calculatedaysperyear() function to calculate no. of days in a year.
 Define calculatehourspermonth() function  to compute hours per month.
 Define an object and call the functions
 Stop

PROGRAM :

class calc:
def getDetail(self):
self.total_computer=258
self.total_hour=6
def calculatesecondsperDay(self):
Second_per_Day=self.total_hour*60*60
print('Total Seconds per Day:',Second_per_Day)
def calculateminutesperWeek(self):
Minutes_per_Week=self.total_hour*60*7
print("Total Minutes per Week:",Minutes_per_Week)
def calculatehourperMonth(self):
Hour_per_Month=self.total_hour*30
print("Total Hour per Month:",Hour_per_Month)
def calculatedayperyear(self):
Day_per_Year=(self.total_hour*365)/24
print("Total Day per Year:",Day_per_Year)
to=calc()
to.getDetail()
to.calculatesecondsperDay()
to.calculateminutesperWeek()
to.calculatehourperMonth()
to.calculatedayperyear()

OUTPUT  :
Total Seconds per Day: 28800
Total Minutes per Week: 3360
Total Hour per Month: 240
Total Day per Year: 121.66666666666667
 

You might also like