Using Python to try to use object-oriented design method to calculate the area and perimeter of graphics

 

Using Python to try to adopt the object-oriented design method.
(1) design a base class Shape:
There are two member functions:
def cal_area(): calculate and return the area of the figure, with two decimal places reserved;
def cal_perimeter(): calculates and returns the perimeter of the graph, retaining two decimal places.
def display(): three lines of string, showing name, area and perimeter respectively, rounding the value and keeping two
Decimal places, as follows:
The name is rect
The area is 6.
The circumference is 10.
There are three variables:
Name: indicates the name, string type;
Area: area, number;
Perimeter: perimeter, number.
(2) three derived classes are designed: Rectangle, Triangle and Circle. The derived classes implement two of the base classes respectively
Operator function.
Rectangle: constructor parameter (n, a, b), n is the name, others are floating-point numbers, two decimal places, a, B points
Don't represent length and width.
Triangle: constructor parameter (n, a, b, c), n is the name, others are floating-point numbers, two decimal places, representing three
The length of the edge.
Circle: constructor parameter (n, a), n is name, a is floating point number, two decimal places, representing radius of circle, circle
The weekly rate is calculated as 3.14.
 

#!/usr/bin/python
# -*- coding: UTF-8 -*-                                                                                                                                                                                                                                                                                                                                                                                                                             
#Topic 8: calculate the area and perimeter of the figure
import math
class Shape:

    def __init__(self,name,area,perimeter):
        self.name = name
        self.area = area
        self.perimeter = perimeter
    def cal_area(self):
        pass
    def cal_perimeter(self):
        pass
    def display(self):
        print('Name:',self.name)
        print('The measure of area:',self.area)
        print('Perimeter:',self.perimeter)
class Rectangle(Shape):
    def __init__(self,n,a,b):
        self.n = n
        self.a=a
        self.b=b
    def cal_area(self):
        area = round (self.a * self.b, 2)
    def cal_perimeter(self,):
        perimeter=round(2*(self.a+self.b),2)

class Triangle(Shape):
    def __init__(self,n,a,b,c):
        self.n=n
        self.a=a
        self.b=b
        self.c=c
    def cal_area(self):
        p=(self.a+self.b+self.c)/2
        area=round(math.sqrt(p*(p-self.a)*(p-self.b)*(p-self.c)),2)
        return area

    def cal_perimeter(self):
        perimeter=self.a+self.b+self.c
        return perimeter

class Circle(Shape):
    def __init__(self,n,a):
        self.n = n
        self.a=a
    def cal_perimeter(self):
        perimeter=round(2*3.14*self.a,2)
        return perimeter
    def cal_area(self):
        area=round(3.14*self.a**2,2)
        return area


if __name__ == '__main__':
    cir=Circle('cir',3)
    result=Shape(cir.n,cir.cal_area(),cir.cal_perimeter())
    result.display()

    ret=Rectangle('ret',4,5)
    result=Shape(ret.n,ret.cal_area(),ret.cal_perimeter())
    result.display()

    tri=Triangle('tri',2,4,5)
    result=Shape(tri.n,tri.cal_area(),tri.cal_perimeter())
    result.display()



If it helps you, please give me a reward! Thank you!

Keywords: Python

Added by bradleyy on Sat, 16 Nov 2019 21:54:25 +0200