当前位置:网站首页>freeCodeCamp----shape_ Calculator exercise

freeCodeCamp----shape_ Calculator exercise

2022-04-23 16:58:00 Lily's autumn

Catalog

1 Subject requirements

1.1 Rectangle class

1.2 Square class

2 Test input

3 Source code analysis


1 Subject requirements

Request to create a Rectangle And a class Square class , The latter is a subclass of the former , Inherit the methods and properties of the former .

The implementation of class is also very clear , Basically translate the requirements into python Just OK 了 , As follows :

1.1 Rectangle class

establish Rectangle Class time , The following methods need to be included :

1.set_width
2.set_height
3.get_area: Returns area (width * height)
4.get_perimeter: Returns perimeter (2 * width + 2 * height)
5.get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
# get_picture Function when the width is less than 50 when , Use the area of the rectangle as * Print out the number 
#  exceed 50 To print an error message 
#  Remember to add a newline character to each line 
6.get_picture():
# get_amount_inside function , Receive a square parameter , Returns the number of squares that can pass through a rectangle 
#  give an example : One 4*8 A rectangle can contain two sides with a length of 4*4 The square of 
7.get_amount_inside();

When Rectangle When the call is a string , The given form is :Rectangle(width=5, height=10)

1.2 Square class

establish Square Class time , There are the following requirements :

1.Square yes Rectangle Subclasses of ;
2. establish Square Class time , Pass in a side length parameter ;
3.__init__(); Methods should be stored from Rectangle Inherited from width and height attribute ;
4.Square Class can call Rectangle Method , And includes a set_side() Method ;
5. When Square When the call is a string , The given form is :Square(side=9)
6.set_width and set_height Method definition width and height;

2 Test input

rect = shape_calculator.Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())

sq = shape_calculator.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)
print(rect.get_amount_inside(sq))

Expected output

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

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

8

3 Source code analysis

There's nothing to say , All definitions , It is not difficult to have only one print function , Try a few more times and you'll come out .

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

    def get_area(self):
        return self.width * self.height;

    def get_perimeter(self):
        return 2 * self.width + 2 * self.height;

    def get_diagonal(self):
        return (self.width ** 2 + self.height ** 2) ** .5;

    def get_picture(self):
        if self.width > 50 or self.height > 50:
            return "Too big for picture."
        else:
            a = ''
            for i in range(self.height):
                # for j in range(self.width):
                # print('*')
                a += '*' * self.width + '\n'
            # print('\n')
            return a

    def __str__(self):
        return 'Rectangle(width={}, height={})'.format(self.width, self.height)

    def get_amount_inside(self, another_shape):
        return (self.width // another_shape.width) * (self.height // another_shape.height)


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

    def set_side(self, side):
        self.width = side
        self.height = side

    def __str__(self):
        return 'Square(side={})'.format(self.width)

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

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

 

版权声明
本文为[Lily's autumn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230555126952.html