Exercise 1

Write a subclass called Rcircle of the superclass Circle.

Requirements

  • Must inherit from Circle
  • Must have it's own constructor. The constructor accepts the circle radius supplied by the user as its argument. That is __init__(self, r).
  • The circle radius must be set in the constructor
  • The Rcircle subclass must reimplement the radius function. It does not make sense for Rcircle to inherit the radius method from Circle since an instance of Rcircle doesn't know anything about the coordinates of the circle.
  • Include the __eq__ special method to compare two circles.

Demo your class.

Your Circle class from last time should have looked something like this:


In [19]:
import numpy as np

class Circle:
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def radius(self):
        self.R = np.sqrt(self.x * self.x + self.y * self.y)
        return self.R
    
    def area(self):
        try:
            self.A = np.pi * self.R* self.R
            return self.A
        except AttributeError:
            r = np.sqrt(self.x * self.x + self.y * self.y)
            self.A = np.pi * r * r
            return self.A
    
    def circum(self):
        try:
            self.C =  2.0 * np.pi * self.R
            return self.C
        except AttributeError:
            r = np.sqrt(self.x * self.x + self.y * self.y)
            self.C = 2.0 * np.pi * r
            return self.C

In [25]:
class Rcircle(Circle):
    def __init__(self,r):
        if r<0:
            raise ValueError ("r<0!")
        self.R=r
    def radius(self):
        return self.R
    def __eq__(self,other):
        return self.R==other.R

In [26]:
r_c=Rcircle(-1)
print(r_c.circum())


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-9036afd23fdc> in <module>()
----> 1 r_c=Rcircle(-1)
      2 print(r_c.circum())

<ipython-input-25-36954fa24e12> in __init__(self, r)
      2     def __init__(self,r):
      3         if r<0:
----> 4             raise ValueError ("r<0!")
      5         self.R=r
      6     def radius(self):

ValueError: r<0!

In [ ]: