-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDay_13.py
147 lines (113 loc) · 2.42 KB
/
Day_13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# coding: utf-8
# # Question 47
#
# ### **Question**
#
# > **_Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area._**
#
# ---
#
# ### Hints
#
# > **_Use def methodName(self) to define a method._**
#
# ---
#
#
#
# **Solutions:**
# In[1]:
class Circle:
def __init__(self, r):
self.radius = r
def area(self):
return 3.1416 * (self.radius ** 2)
circle = Circle(5)
print(circle.area())
# ---
#
# # Question 48
#
# ### **Question**
#
# > **_Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area._**
#
# ---
#
# ### Hints
#
# > **_Use def methodName(self) to define a method._**
#
# ---
#
#
#
# **Solutions:**
# In[2]:
class Rectangle:
def __init__(self, l, w):
self.length = l
self.width = w
def area(self):
return self.length * self.width
rect = Rectangle(2, 4)
print(rect.area())
# ---
#
# # Question 49
#
# ### **Question**
#
# > **_Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default._**
#
# ---
#
# ### Hints
#
# > **_To override a method in super class, we can define a method with the same name in the super class._**
#
# ---
#
#
#
# **Solutions:**
# In[3]:
class Shape:
def __init__(self):
pass
def area(self):
return 0
class Square(Shape):
def __init__(self, length=0):
Shape.__init__(self)
self.length = length
def area(self):
return self.length * self.length
Asqr = Square(5)
print(Asqr.area()) # prints 25 as given argument
print(Square().area()) # prints zero as default area
# ---
#
# # Question 50
#
# ### **Question**
#
# > **_Please raise a RuntimeError exception._**
#
# ---
#
# ### Hints
#
# > **_Use raise to raise an exception._**
#
# ---
#
# **Solution:**
# In[4]:
raise RuntimeError("something wrong")
# ---
#
# ## Conclusion
#
# **_Well It seems that the above problems are very much focused on basic concpets and implimantation of object oriented programming.As the concepts are not about to solve any functional problem rather design the structure , so both codes are very much similar in there implimantation part._**