I am writing Python program which calculates area and perimeter of various 2d shapes. I am trying to learn about OOP and this is the first project which I wrote from scratch.
I'm wondering how to let user now what attributes he should use in order to get the correct result from particular class? For ex:
C = Circle(radi=2)
gives me an atttribute error because the correct name should be radius not radi. I would like to handle this error and print the correct attribute name.
from math import pi
"""
Exercise (Shape Area and Perimeter Classes):
Create an abstract class called Shape and then inherit from it other shapes like
diamond, rectangle, cirlce, etc. Then have each class override the area and
perimeter functionality to handle each shape type.
I wrote 20 classes to handle various shapes
Geometry figures taken from: http://www.aplustopper.com/mensuration-rs-aggarwal-class-7-maths-solutions-exercise-20c/
"""
"""
init method takes all arguments in key=value format
str method returns all given attributes in column
"""
class Shape:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __str__(self):
all_info_str = ""
for attribute, value in self.__dict__.items():
if value > 0:
all_info_str += attribute + ": " + "%.2f" %(value) + "\n"
return self.__class__.__name__ + "\n" + all_info_str
"""
Rectangle class attributes must be:
lenght=x, width=x
"""
class Rectangle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = self.lenght * self.width
def count_perimeter(self):
self.perimeter = 2 * (self.lenght + self.width)
"""
Square class attributes must be:
lenght=x
"""
class Square(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = self.lenght ** 2
def count_perimeter(self):
self.perimeter = 4 * self.lenght
"""
Triangle class attributes must be:
base=x, height=x, leg_a=x, leg_b=x
"""
class Triangle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = 1/2 * self.base * self.height
def count_perimeter(self):
self.perimeter = self.leg_a + self.base + self.leg_b
"""
RightTriangle class attributes must be:
base=x, height=x, hypotenuse=x
"""
class RightTriangle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = self.base + self.height + self.hypotenuse
def count_perimeter(self):
self.perimeter = 1/2 * self.base * self.height
"""
EquilateralTriangle class attributes must be:
leg_a=x
"""
class EquilateralTriangle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = math.sqrt(3) / 4 * self.leg_a ** 2
def count_perimeter(self):
self.perimeter = 3 * self.leg_a
"""
IsoscelecRightTriangle class attributes must be:
leg_a=x, hypotenuse=x
"""
class IsoscelecRightTriangle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = 1/2 * self.leg_a ** 2
def count_perimeter(self):
self.perimeter = 2 * self.leg_a + self.hypotenuse
"""
Parallelogram class attributes must be:
lenght=x, height=x, width=x
"""
class Parallelogram(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = self.lenght * self.height
def count_perimeter(self):
self.perimeter = 2 * (self.lenght + self.width)
"""
Rhombus class attributes must be:
hypotenuse_1=x, hypotenuse_2=x, lenght=x
"""
class Rhombus(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = 1/2 * self.hypotenuse_1 * self.hypotenuse_2
def count_perimeter(self):
self.perimeter = 4 * self.lenght
"""
Trapezium class attributes must be:
height=x, top=x, base=x, leg_a=x, leg_b=x
"""
class Trapezium(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = 1/2 * self.height * (self.top + self.base)
def count_perimeter(self):
self.perimeter = self.leg_a + self.leg_b + self.top + self.base
"""
Circle class attributes must be:
radius=x
"""
class Circle(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = pi * self.radius ** 2
def count_perimeter(self):
self.perimeter = (pi * self.radius) + (2 * self.radius)
"""
Ring class attributes must be:
radius_1=x, radius_2=x
"""
class Ring(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = pi * (self.radius1 - self.radius2)
def count_perimeter(self):
self.perimeter = (pi * 2 * self.radius1) - (pi * 2 * self.radius2)
"""
CircleSector attributes must be:
angle=x, radius=x
"""
class CircleSector(Shape):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def count_area(self):
self.area = self.angle / 360 * pi * self.radius ** 2
def count_perimeter(self):
self.perimeter = (self.angle / 360 * 2 * pi * self.radius) +\
2 * self.radius
if __name__ == '__main__':
R = Rectangle(lenght=2, width=5)
R.count_area()
R.count_perimeter()
print(R)
C = Circle(radius=23)
C.count_area()
C.count_perimeter()
print(C)