Python打卡Day13

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.


Main author's Solution: Python 2

1class Circle(object):
2    def __init__(self, r):
3        self.radius = r
4
5    def area(self):
6        return self.radius**2*3.14
7
8aCircle = Circle(2)
9print aCircle.area()

My Solution: Python 3

 1class Circle():
 2    def __init__(self,r):
 3        self.radius = r
 4
 5    def area(self):
 6        return 3.1416*(self.radius**2)
 7
 8
 9circle = Circle(5)
10print(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.


Main author's Solution: Python 2

 1class Rectangle(object):
 2    def __init__(self, l, w):
 3        self.length = l
 4        self.width  = w
 5
 6    def area(self):
 7        return self.length*self.width
 8
 9aRectangle = Rectangle(2,10)
10print aRectangle.area()

My Solution: Python 3

 1class Rectangle():
 2    def __init__(self,l,w):
 3        self.length = l
 4        self.width = w
 5
 6    def area(self):
 7        return self.length*self.width
 8
 9
10rect = Rectangle(2,4)
11print(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.


Main author's Solution: Python 2

 1class Shape(object):
 2    def __init__(self):
 3        pass
 4
 5    def area(self):
 6        return 0
 7
 8class Square(Shape):
 9    def __init__(self, l):
10        Shape.__init__(self)
11        self.length = l
12
13    def area(self):
14        return self.length*self.length
15
16aSquare= Square(3)
17print aSquare.area()

My Solution: Python 3

 1class Shape():
 2    def __init__(self):
 3        pass
 4
 5    def area(self):
 6        return 0
 7
 8class Square(Shape):
 9    def __init__(self,length = 0):
10        Shape.__init__(self)
11        self.length = length
12
13    def area(self):
14        return self.length*self.length
15
16Asqr = Square(5)
17print(Asqr.area())      # prints 25 as given argument
18
19print(Square().area())  # prints zero as default area

Question 50

Question

Please raise a RuntimeError exception.


Hints

UUse raise() to raise an exception.


Solution:

1raise 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.

go to previous day

go to next day

Discussion