Polygon Area Calculator - Yet another question

Here’s my problem: I am being able to create a parent class for Rectangle with the methods to set up width and height like this:

class Rectangle:
  def __init__(self, width, height):
    self.width = width
    self.height = height

  def set_width(self, width):
    self.width = width

  def set_height(self, height):
    self.height = height

I am not including the other methods. From this parent class I am creating the class Square like this:

class Square(Rectangle):
  def __init__(self, side):
        # equivalent to `Rectangle.__init__(self, side, side)`
        self.side = side
        super().__init__(side, side)
  def set_side(self, side):
      self.side = side
  def __str__(self):
    return "Square(side={s})".format(s = self.side)

Now, here is where things get strange: Whenever I use the set_side() in the Square class, it is not updating my instance of the object and keeping the original side…

Please someone tell me: What am I doing wrong?

Sure:

# Reactangle and Square classes

class Rectangle:
  def __init__(self, width, height):
    self.width = width
    self.height = height

  def __str__(self):
    return "Rectangle(width={w}, height={h})".format(h=self.height, w=self.width)
  
  def set_width(self, width):
    self.width = width

  def set_height(self, height):
    self.height = height

  def get_area(self):
    self.area = self.width*self.height
    return self.area
  
  def get_perimeter(self):
    self.perimeter = 2 * self.width + 2 * self.height
    return self.perimeter
  
  def get_diagonal(self):
    self.diagonal = (self.width ** 2 + self.height ** 2) ** 0.5
    return self.diagonal
  
  def get_picture(self):
    a = ""
    pic = ''''''
    if self.width >= 50 or self.height >= 50:
      return "Too big for picture."
    else:
      for i in range(self.width):
        a += "*"
      for j in range(self.height):
        pic += a + "\n"
      return pic
      
  def get_amount_inside(self, other_shape):
       if not isinstance(other_shape, Rectangle):
           return f"{other_shape} is not the right type of object."
       else:
           width_units = self.width/other_shape.width
           return other_shape.width

class Square(Rectangle):
  def __init__(self, side):
        # equivalent to `Rectangle.__init__(self, side, side)`
        self.side = side
        super().__init__(side, side)
          
  def set_side(self, side):
    self.side = side

  def __str__(self):
    return "Square(side={s})".format(s = self.side)
    
rect = Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())

sq = Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())

rect.set_height(8)
rect.set_width(16)

In the code I am creating a rectangle rect with a width of 10 and a height of 5. I get the right output after resetting the height to 3 and the perimeter is also correct after this modification… The code also prints correctly the characteristics of the object and draws a nice picture… Swell… See it here:

50
26
Rectangle(width=10, height=3)
**********
**********
**********

With the square though, I don’t get what’s expected for the diagonal calculation:

I create the instance of square sq with a size of 9. Swell, the area is 81… Then I change the side to 4 and the diagonal is wrong as it asumes that the square side is still 9 (diagonal is 12.73) and paints a picture of square with side 9… The print statement of the new square with side 4 is right…

81
12.727922061357855
Square(side=4)
*********
*********
*********
*********
*********
*********
*********
*********
*********

You are not going to be proud of what I did… It is just horrible but it runs:

  def set_side(self, side):
    Rectangle.set_width(self, side)
    Rectangle.set_height(self, side)
    self.side = (self.get_area())**0.5

:grimacing:

Finally…

It passed all the tests…

So what I did is wrong?

  def set_side(self, side):
    Rectangle.set_width(self, side)
    Rectangle.set_height(self, side)

Man! Yours also work! I just used the two inherited methods from the parent class to define another… Am I a terrible person? (Please, don’t answer the question :laughing:)

Makes more sense now. Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.