Python

Class 예제(3) - 원통의 표면적, 부피

baecode 2022. 5. 17. 17:53
반응형

원통의 부피 공식

 

Volume of cylinder=πr²h

 

3.14 * (radius**2) * height

 

원통의 표면적 공식

 

Surface Area of a Cylinder = 2πr² + 2πrh

2*(3.14*(radius**2)) + 2*(3.14 * radius * height)

 

 

class Cylinder:
    
    pi = 3.14

    def __init__(self,height=1,radius=1):
        
        self.height = height
        self.radius = radius
    
    #부피
    
    def volume(self):
    	
        #round로 소수점 2자리까지 출력
        return round(Cylinder.pi * (self.radius**2) * self.height, 2)
    
    #표면적
    
    def surface_area(self):
    
        return 2*(Cylinder.pi*(self.radius**2)) + 2*(Cylinder.pi*self.radius*self.height)

 

결과

C = Cylinder(2,3)

C.volume()

#56.52

C.surface_area()

#94.2
반응형

'Python' 카테고리의 다른 글

[Python] Map  (2) 2022.09.01
Immutable, Mutable  (0) 2022.06.07
Class 예제(2) - 두 점 사이의 거리, 기울기  (0) 2022.05.17
Class 예제 (1) - 은행 계정 클래스  (0) 2022.05.17
OOP (객체지향 프로그래밍)  (0) 2022.05.17