Exp 2.1a) The floor of the white house consists of many squares and rectangle. Tom wants to name single function of different perspective to calculate the area of squares and rectangle. Help tom to achieve his objective by means of polymorphism. Aim: Program to illustrate polymorphism Algorithm: Step1: create a class named rectangle Step2: Input length and breadth Step3: create a function area Step4: calculate area =length x breadth Step5: return the value area Step6: create a class square Step7: Input side value Step8: create a function area Step9: calculate area =side x side Step10: return the value area Program: class Rectangle: def __init__(self, length, breadth): self.l = length self.b = breadth def area(self): return self.l * self.b class Square: def __init__(self, side): self.s = side def area(self): return self.s ** 2 rec = Rectangle(5, 20) squ = Square(9) print("Area of rectangle is: ", rec.area()) print("Area of square is: ", squ.area()) output Area of rectangle is: 100 Area of square is: 81 Exp 2.1b) Imagine a car. Its components such as steering, wheel, brake, accelerator are all visible and can be accessed directly. Car engine was encapsulated so that we can’t access it freely. Image these real time cirumstances develop a python program to perform the concept of encapsulation. Algorithm: Step 1: create class car Step 2: declare a variable a Step 3: declare a variable double underscore c Step 4: assign a= steering Step 5: assign c=engine Step 6: create a object ob1 for class car Step 7: access the variable a Step 8: access the variable double underscore c Program: # program to implement encapsulation class car: def __init__(self): self.a = "steering-accessing normal variable" self.__c = "engine" #double underscore before the variable makes it as private obj1 = car() print(obj1.a) print("try to access private variable but is shows............error as") print(obj1.__c) # try to access private variable output: steering-accessing normal variable try to access private variable but is shows............error as Traceback (most recent call last): File "<string>", line 10, in <module> ERROR! AttributeError: 'car' object has no attribute '__c' Exp 2.2a) Implement Data Abstraction and Inheritance. Aim: Program to illustrate Data Abstraction and Inheritance Program: #from abc import ABC class llgm(ABC): #abstract classdef calculate_area(self): #abstract methodpass pass class Square(llgm): length = 5 def Area(self): return self.length * self.length class Circle(llgm): radius =4 def Area(self): return 3.14 * self.radius * self.radius sq = Square() #object created for the class ‘Square’ cir = Circle() #object created for the class ‘Circle’ print("Area of a Square:", sq.Area()) #call to ‘calculate_area’ method defined inside the class ‘Square’ print("Area of a circle:", cir.Area()) Exp 2.2b #from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass #Sub class/ child class of abstract class class SBI(Bank): def interest(self): "Implementation of abstract method" print("In sbi bank 5 rupees interest") s= SBI() s.bank_info () s.interest() Exp :2.3 Differentiate Method Overloading and Overriding. AIM: To Differentiate Method Overloading and Overriding. ALGORITHM: Method Overloading: 1) We create a class with one method sayHello(). 2) The first parameter of this method is set to None. 3) This gives us the option to call it with or without a parameter. 4) An object is created based on the class 5) we call its method using zero and one parameter. 6) To clarify method overloading, we can now call the method sayHello() in two ways: i)obj.sayHello() ii)obj.sayHello('cse') program: class Human: def sayHello(self, name=None): if name is not None: print('Hello ' + name) else: print('Hello ') # Create instance obj = Human() # Call the method obj.sayHello() # Call the method with a parameter obj.sayHello('cse') output: Hello Hello cse ALGORITHM Method overriding: 1) created an employee class 2) which contains a message function that prints a message. 3) Next, we created a department that inherits from the Employee class. 4) Within this, we created a function with the same name message with a different print message. 5) Here, we are just overriding the message. Next, we created an Object for Employee and Department class and calling that message function. 6) The emp object is printing a string from the Employee message function. Whereas dept.message() is a printing test from Department. Program: class Employee: def message(self): print('This message is from Emp') class Department(Employee): def message(self): print('This Department is inherited from Emp') emp = Employee() emp.message() print('------------') dept = Department() dept.message() output: This message is from Emp -----------This Department is inherited from Emp EX.No.2.5 Aim: Create a module called "math_operations.py" with a class called "Calculator." Import the "Calculator" class into another script and use its methods to perform mathematical operations.. Algorithm Step 1: A module can contain functions, classes, and variables that can be used in other parts of your program. Step 2: In , the module “math_operations.py” contains a function called “add” ,”Sub”,”multiply”,divide…etc and a class called “calc”. These can be imported and used in other Python scripts with the class calc(). Step 3: Use the add() function to work with values assigned to the function in Python Step 4: Before use the methods , need to import the module first using import command Step 5: Run the code and execute the output Program # import math_operations.py class calculator(): def __init__(self,a,b): self.a=a self.b=b def add(self): return self.a+self.b def mul(self): return self.a*self.b def div(self): return self.a/self.b def sub(self): return self.a-self.b #from math_operations.py import add, mul, div , sub a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) obj=calculator(a,b) choice=1 while choice!=0: print("0. Exit") print("1. Add") print("2. Subtraction") print("3. Multiplication") print("4. Division") choice=int(input("Enter choice: ")) if choice==1: print("Result: ",obj.add()) elif choice==2: print("Result: ",obj.sub()) elif choice==3: print("Result: ",obj.mul()) elif choice==4: print("Result: ",round(obj.div(),2)) elif choice==0: print("Exiting!") else: print("Invalid choice!!") print() Output Enter first number: 12 Enter second number: 12 0. Exit 1. Add 2. Subtraction 3. Multiplication 4. Division Enter choice: 1 Result: 24 0. Exit 1. Add 2. Subtraction 3. Multiplication 4. Division Enter choice: 2 Result: 0 0. Exit 1. Add 2. Subtraction 3. Multiplication 4. Division Enter choice: 3 Result: 144 0. Exit 1. Add 2. Subtraction 3. Multiplication 4. Division Enter choice: 4 Result: 1.0 0. Exit 1. Add 2. Subtraction 3. Multiplication 4. Division Enter choice: 0 Exiting!