• Click below to download Unit 3 programs in pdf file: -



Unit 3


""" 1. Write a program to create a Student class with name,

age and marks as data members. Also create a method

named display() to view the student details. Create an

object to Student class and call the method using the

object. """


class student:

    def __init__(self):

        self.name = 'Peter'

        self.age = 20

        self.marks = 80

        

    def display(self):

        print("Hello",self.name)

        print("Your Marks",self.marks)

        print("Your age is ",self.age)


std = student()

std.display()



""" 2. Write a program to create Student class with a

constructor having more than one parameter. """


class Student:

    def __init__(self,name="Rakesh",marks=55):

        self.name=name

        self.marks=marks

    def display(self):

        print("Student Name:",self.name)

        print("Student Marks:",self.marks)

s=Student()

s.display()

print("______________")

s1=Student('Rahul',70)

s1.display()



""" 3. Write a program to demonstrate the use of instance and

class/static variables. """


class emp:

    def __init__(self):

        self.x = 10


    def modify(self):

        self.x = 15


e1 = emp()

print("Initial d1 x = ",e1.x)

e2 = emp()

print("Initial d2 x = ",e2.x)

e1.modify()

print("D1 x after update",e2.x)

e2.modify()

print("D1 x after update",e2.x)



""" 4. Write a program to store data into instances using

mutator methods and to retrieve data from the instances

using accessor methods. """


class Student:

    def setname(self,name):

        self.name = name


    def getname(self):

        return self.name


    def setmarks(self,marks):

        self.marks = marks


    def getmarks(self):

        return self.marks


n = int(input("Enter data of student "))

i = 0


while(i < n):

    print("Create student class instance from mutator method ")

    s = Student()


    name = input("Enter the name : ")

    s.setname(name)


    marks = int(input("Enter the marks : "))

    s.setmarks(marks)


    print("Retrieve student class instance from accessor method ")

    print("Hello ",s.getname())

    print("Your marks are ",s.getmarks())

    i += 1

    print("............\n")



""" 5. Write a program to use class method to handle the

common features of all the instance of Student class. """


class Student:

    marks = 500

    @classmethod

    def display(cls,name):

        print("{} student acheived {} marks ".format(name,cls.marks))


Student.display("Ronny")

Student.display("Jonny")



""" 6. Write a program to create a static method that counts

the number of instances created for a class. """


class cls():

    n = 0

    def __init__(self):

        cls.n = cls.n + 1


    @staticmethod

    def noobj():

        print("No of instances created ",cls.n)


obj1 = cls()

obj2 = cls()

cls.noobj()



""" 7. Create a Bank class with two variables name and

balance. Implement a constructor to initialize the

variables. Also implement deposit and withdrawals

using instance methods. """


import sys


class Bank(object):

    def __init__(self,name,bal=0.0):

        self.name = name

        self.bal = bal


    def deposit(self,amount):

        self.bal += amt

        return self.bal


    def withdrawal(self,amt):

        if(amt > self.bal):

            print("Balance amount is less than withdrawal amount")

        else:

            self.bal -= amt

            return self.bal


name = input("Enter name : ")


b = Bank(name)


while(True):

    print("d = deposit, w = withdrawal, e = exit")

    choice = input("Input your choice : ")


    if(choice == 'e' or choice == 'E'):

        sys.exit()


    amt = float(input("Enter amount : "))


    if(choice == 'd' or choice == 'D'):

        print("Balance after deposit ",b.deposit(amt))


    elif(choice == 'w' or choice == 'W'):

        print("Balance after withdrawal is ",b.withdrawal(amt))



""" 8. Write a program to create a Emp class and make all the

members of the Emp class available to another class

(Myclass). [By passing members of one class to another] """


class Emp:

    def __init__(self,id,name,salary):

        self.id=id

        self.name=name

        self.salary=salary

    def display(self):

        print("Your id is:",self.id)

        print("Your name is:",self.name)

        print("Your salary is:",self.salary)

class Myclass:

    @staticmethod

    def mymethod(e):

        e.salary+=5000

        e.display()

e=Emp(10,"Rahul",71000)

e.display()

Myclass.mymethod(e)



""" 9. Create a Student class to with the methods set_id,

get_id, set_name, get_name, set_marks and get_marks

where the method name starting with set are used to

assign the values and method name starting with get are

returning the values. Save the program by student.py.

Create another program to use the Student class which

is already available in student.py."""


from unit3p9student import Student

s=Student()

s.setid(10)

s.setname("Rohit")

s.setcity("Gujarat")

s.setmarks(92)

print("Id:",s.getid())

print("Nmae:",s.getname())

print("City:",s.getcity())

print("Marks:",s.getmarks())



#Save below code with 'p9student' file name in same folder


class Student:

    def setid(self,id):

        self.id=id

    def getid(self):

        return self.id

    def setname(self,name):

        self.name=name

    def getname(self):

        return self.name

    def setcity(self,city):

        self.city=city

    def getcity(self):

        return self.city

    def setmarks(self,marks):

        self.marks=marks

    def getmarks(self):

        return self.marks



""" 10. Write a program to access the base class constructor

from a sub class by using super() method and also

without using super() method. """


class Father:

    def __init__(self,property=0):

        self.property = property

    def display_property(self):

        print("Father's property ",self.property)


class Son(Father):

    def __init__(self,property1=0,property=0):

        super().__init__(property)

        self.property1 = property1

    def display_property(self):

        print("Total property of child : ",self.property1 + self.property)


s = Son(25000,80000)

s.display_property()


#without using super() method

print("\nWithout using super() method")


class Father1:

    def __init__(self):

        self.property = 80000


    def display_property1(self):

        print("Father's property : ",self.property)


class Son1(Father1):

    pass


s =Son1()

s.display_property1()



""" 11. Write a program to override super class constructor and

method in sub class. """


class Father:

    def __init__(self):

        self.property = 90000

    def display(self):

        print("Father's property is ",self.property)


class  Son(Father):

    def __init__(self):

        self.property = 25000

    def display(self):

        print("Son's property is ",self.property)


s = Son()

s.display()


""" 12. Write a program to implement single inheritance in

which two sub classes are derived from a single base

class. """


class Bank(object):

    cash = 1000

    @classmethod

    def avail_cash(cls):

        print(cls.cash)


class BobBank(Bank):

    pass


class StateBank(Bank):

    cash = 20000

    @classmethod

    def avail_cash(cls):

        print(cls.cash + Bank.cash)


b = BobBank()

b.avail_cash()


s = StateBank()

s.avail_cash()



""" 13. Write a program to implement multiple inheritance

using two base classes. """


class Father:

    def Height(self):

        print("Height is 6.0ft")


class Mother:

    def Iq(self):

        print("IQ is 130")


class Child(Father,Mother):

    pass


c = Child()

print("Child inherited Qualties")


c.Height()

c.Iq()



""" 14. Write a program to understand the order of execution of

methods in several base classes according to method

resolution order (MRO). """


class A(object):

    def method(self):

        print("A class method")

        super().method()


class B(object):

    def method(self):

        print("B class method")

        super().method()


class C(object):

    def method(self):

        print("C class method")


class X(A,B):

    def method(self):

        print("X class method")

        super().method()


class Y(B,C):

    def method(self):

        print("Y class method")

        super().method()


class P(X,Y,C):

    def method(self):

        print("P class method")

        super().method()


p = P()

p.method()



""" 15. Write a program to check the object type to know

whether the method exists in the object or not. """


class Dog:

    def bark(self):

        print("Bow wow")


class Duck:

    def talk(self):

        print("Quack quack")


class Human:

    def talk(self):

        print("Hello")


def call_talk(obj):

    if hasattr(obj,'talk'):

        obj.talk()

    elif hasattr(obj,'bark'):

        obj.bark()

    else:

        print("Incorrect object passed")


class Whale:

    pass


x = Duck()

call_talk(x)


x = Human()

call_talk(x)


x = Whale()

call_talk(x)



""" 16 Write a program to overload the addition operator (+) to

make it act on the class objects. """


class BookX:

    def __init__(self,pages):

        self.pages = pages

    def __add__(self,other):

        return self.pages + other.pages


class BookY:

    def __init__(self,pages):

        self.pages = pages


b1 = BookX(100)

b2 = BookY(150)

print("Total pages : ",b1+b2) 



""" 17. Write a program to show method overloading to find sum

of two or three numbers. """


class MyClass:

    def sum(self,a=None,b=None,c=None):

        if a!=None and b!=None and c!=None:

            print("Sum of three numbers : ",a+b+c)

        elif a!= None and b!= None:

            print("Sum of two numbers : ",a+b)

        else:

            print("Enter two or three arguments ")


m = MyClass()

m.sum(10,20,30)

m.sum(10.5,20.55)

m.sum(200)



""" 18. Write a program to override the super class method in

subclass. """


import math


class Square:

    def area(self,x):

        print("Area of a square is %2f"%(x*x))


class Circle(Square):

    def area(self,x):

        print("Area of a circle is %2f"%(math.pi*x*x))


c = Circle()

c.area(15)


c1 = Square()

c1.area(15)