(PP-7)Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a constructor which takes the parameters x and y (these should all be numbers). i. Write a method called add which returns the sum of the attributes x and y. ii. Write a class method called multiply, which takes a single number parameter a and returns the product of a and MULTIPLIER. iii. Write a static method called subtract, which takes two number parameters, b and c, and returns b - c. iv. Write a method called value which returns a tuple containing the values of x and y. Make this method into a property, and write a setter and a deleter for manipulating the values of x and y.
7A.#calculator.py
class number:
multiplier=3
def __init__(self,x,y):
self.x=x
self.y=y
def add(self):
return self.x+self.y
def multiply(self,a):
return self.multiplier*a
def subtract(b,c):
return b-c
T=number(2,4)
print(T.add())
print(T.multiply(2))
print(number.subtract(4,2))
7B.design the class that stores the info of students and display the same
class student: def __init__(self,name,roll_no,address): self.name=name self.roll_no=roll_no self.address=address s=student("Bhavesh",406,"malad") print(s.name) print(s.roll_no) print(s.address)
7C.create a module student_details with functions display_name, marks_total & create new file that imports the module and display the functions.
#student_details.py def display_name(Name): print("student Name:",Name) return def marks_total(p,c,m): print("Total marks obtained by student:",p+c+m) return
#student.py
from module1 import*
display_name("aashish")
marks_total(80,40,60)
7D.create a module calculate_Area with functions circle_Area, triangle_Area, rectangle_Area & create new file that imports the module and display the functions.
#calculate_Area.py
import math
def Circle_Area(radius):
AOC=math.pi*radius*radius
print("Area of circle:",AOC)
return
def triangle_Area(base,height):
AOT=(1/2)*base*height
print("Area of triangle:",AOT)
return
def rectangle_Area(l,b):
AOR=l*b
print("Area of rectangle:",AOR)
return
#Area.py
from calculate_Area import*
Circle_Area(2)
triangle_Area(3,5)
rectangle_Area(4,8)
7E.write program to implement the concept of Inheritance single level, Multilevel, Multiple Inheritance.
#_Single Inheritance
class parent: def method1(self): print("****single level inheritnce****") print("parent method") class child(parent): def method2(self): print("calling child method") c=child() c.method1() c.method2()
#_Multilevel Inheritance
class parent: def method1(self): print("****multilevel level inheritnce****") print("parent method") class child1(parent): def method2(self): print("calling child1 method") class child2(child1): def method3(self): print("calling child2 method") c=child2() c.method1() c.method2() c.method3()
#_Multiple Inheritance
class parent1: def method1(self): print("**** multiple inheritnce****") print("calling parent1 method") class parent2(parent1): def method2(self): print("calling parent2 method") class child(parent2): def method3(self): print("calling child method") c=child() c.method1() c.method2() c.method3()
7F.class vehicle
class vehicle: def __init__(self,brand,model,type1): self.brand=brand self.model=model self.type1=type1 self.gas_tank_size=14 self.fuel_level=0 def fuel_up(self): self.fuel_level=self.gas_tank_size print("Gas tank is now full") def drive(self): print("is now driving ",self.brand,self.model) def update_fuel_level(self,new_level): if new_level<=self.gas_tank_size: self.fuel_level=new_level print(self.fuel_level) else: print("Exceeded capacity") def get_gas(self,amount): if(self.fuel_level+amount<=self.gas_tank_size): self.fuel_level+=amount print("Added fuel") else: print("The tank wont hold that much") v=vehicle("Honda","city","petrol") v.drive() v.fuel_up() v.get_gas(3) v.update_fuel_level(5)
7G.program to demonstrate the use of super keywordclass parent: def method(self): print("parent method") class child(parent): def method(self): print("calling child method") super().method() c=child() c.method()7H.program to implement the concept of data hiding with private variables
class justcounter: __secretcount=0 def count(self): self.__secretcount+=1 print(self.__secretcount) counter=justcounter() counter.count() counter.count() print(counter.__secretcount)
7I.demonstrate the concept of static method
class staticdemo: @staticmethod def statmethod(): print("Static Method") def nonstatmethod(self): print("Non Static Method") s=staticdemo() s.nonstatmethod() staticdemo.statmethod()
7J.program to define class My_class with static method get_max_value that takes two parameters x&y and returns the max value
class Myclass: def __init__(self): self=0 @staticmethod def get_max_value(x,y): return max(x,y) obj=Myclass() print(Myclass.get_max_value(20,30))
print(obj.get_max_value(20,30))
Comments
Post a Comment