730Gr12 CSC Class and Objects Worksheet1
730Gr12 CSC Class and Objects Worksheet1
April 2017
Classes and objects- Worksheet 1
1. Find o/p
class counter :
overall_total=0
def __init__(self):
self.mytotal=0
def increment(self):
counter.overall_total=counter.overall_total+1
self.mytotal=self.mytotal+1
def __str__(self):
string="overall total : "+str(counter.overall_total)
string=string + "\tMy total : "+ str(self.mytotal)
return string
a=counter()
b=counter()
a.increment()
b.increment()
print a
b.increment()
print a, b
2. Find o/p
class myclass:
def __init__(self,name='x',v=[1,2,3]):
self.val=v
self.name=name
def __str__(self):
return self.name + ":" +str(self.val)
def method1(self,limit=3):
result=[]
for v in self.val:
if v>=limit and v not in result:
result.append(v)
return result
def method2(self,test_val):
return self.val.count(test_val)
inst1=myclass()
print inst1
inst2=myclass("rich",[3,5,3,3,3,4,5])
print inst2
print inst2.method1()
print inst2.method2(3)
3 Find o/p
class contact:
all_contacts=[]
def __init__(self,name,email):
em=[]
em.append(name)
em.append(email)
contact.all_contacts.append(em)
print(contact.all_contacts)
print(self.all_contacts)
myid=contact("amit","[email protected]")
4. Find o/p
class point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getdistance(self):
from math import sqrt
dist =sqrt(self.x*self.x+self.y*self.y)
return dist
p1=point()
p2=point()
p3=point(3,3)
p4=p2
v1=p1.x
v2=p2.y
v3=p2==p1
v4=p2.getdistance()
v5=p2==p4
print v1,v2,v3,v4,v5
5 Find o/p
class myclass:
def __init__(self,val=0):
self.L=[]
for i in range(val):
self.L.append(i)
def __str__(self):
return 'L:%s'%(str(self.L))
def method1(self,x=0):
for i in range(len(self.L)):
self.L[i]=x*self.L[i]
return self.L
def method2(self,i=0):
if i in range(len(self.L)):
return self.L[i]
else:
return None
inst1=myclass()
print inst1
inst2=myclass(3)
print inst2
print inst2.method1(3)
print inst2.method2(2)
Give o/p again for the same code with no arguments passed to last 2 statements
6. Give O/p after correcting any errors :
class student:
def __init__(self,name,reg):
self.name=name
self.regno=reg
def __str__(self):
print self.regno,self.name
s1=student()
s2=student(raw_input(" Name : "),raw_input("Registration number : "))
s3=student(raw_input(" Name : "),int(raw_input("Registration number : ")))
print s1
print s2
print s3
7. Give o/p
class E2:
def __init__(self,paint,make):
self.paint=paint
self.make=make
def printmycar(self):
print "I'm driving a ",self.paint,self.make," car"
car1=E2("red","Mercedes")
car2=E2("blue","Audi")
car1.paint=car2.make
car2.make=car1.make
car1.printmycar()
car2.printmycar()
8. Find o/p
class myclass:
def __init__(self,val=0):
self.L=[]
for i in range(val):
self.L.append(i)
def __str__(self):
return 'L:%s'%(str(self.L))
def method1(self,x=0):
for i in range(len(self.L)):
self.L[i]=x*self.L[i]
return self.L
def method2(self,i=0):
if i in range(len(self.L)):
return self.L[i]
else:
return None
inst1=myclass()
print inst1
inst2=myclass(3)
print inst2
print inst2.method1(2)
print inst2.method2(1)
9. Rewrite after correcting errors , if any
class student:
def __init__(self,name,admno):
self.__x=name
self.y=admno
def __displaydata(self):
print self.__x,self.y
stud1=student()
stud1.__displaydata()
print stud1.__x
print stud1.y
10 class bag:
def __init__(self,p): # function 1
self.pockets=p
def company(self):
print ("The company of bag is VIP")
B=bag(10)
print B
(i) what is function 1 and when is this function called /invoked?
(ii) what is function 2 and when is this function called/invoked?
11 Give o/p and justify
class bag:
def __init__(self): # function 1
self.pockets=0
def company(self):
print ("The company of bag is VIP")