도형 넓이 클래스


답안 제출

Points: 1
시간 제한: 2.0s
메모리 제한: 1G

문제 유형
허용된 언어
Python

Shape을 상속받은 직사각형과 삼각형이 자신의 넓이를 계산하도록 메서드를 오버라이딩하시오.

미리 작성된 코드

다음 부모 클래스는 제출 코드 앞에 자동으로 추가된다. 부모 클래스는 제출하거나 다시 작성하지 않는다.

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

    def get_size(self):
        return (self.width, self.height)

    def get_area(self):
        return 0

작성할 클래스

다음 자식 클래스만 작성한다.

class Rectangle(Shape):
    def get_area(self):
        # 코드를 작성하세요.


class Triangle(Shape):
    def get_area(self):
        # 코드를 작성하세요.
  • Shapewidthheight를 저장한다.
  • get_size()(width, height) 형태의 tuple을 반환한다.
  • Shape.get_area()는 0을 반환한다.
  • Rectangle.get_area()는 \(width \times height\)를 반환한다.
  • Triangle.get_area()는 \(width \times height \div 2\)를 반환한다.
  • 모든 넓이의 반환 자료형은 int이다.

클래스를 사용하는 코드

작성한 클래스들은 아래 코드와 함께 실행된다.

n = int(input())
shapes = []

for _ in range(n):
    shape_type, width, height = input().split()
    width = int(width)
    height = int(height)
    if shape_type == "Shape":
        shapes.append(Shape(width, height))
    elif shape_type == "Rectangle":
        shapes.append(Rectangle(width, height))
    else:
        shapes.append(Triangle(width, height))

q = int(input())

for _ in range(q):
    command = input().split()
    shape = shapes[int(command[1]) - 1]

    if command[0] == "size":
        result = shape.get_size()
    else:
        result = shape.get_area()

    print(result)
    print(type(result))

위 코드가 정상적으로 동작하도록 작성할 자식 클래스의 정의 전체만 제출한다. 미리 작성된 부모 클래스, 입력을 받는 코드, 객체를 생성하는 코드는 제출하지 않는다.

상태만 변경하는 메서드의 반환값은 출력하지 않는다. 값을 조회하거나 계산 결과를 반환하는 메서드는 반환값과 자료형을 출력한다.

입력

첫째 줄에 객체 수 \(N\)이 주어진다. 다음 \(N\)개의 줄에는 Shape, Rectangle, Triangle 중 하나와 width, height가 주어진다.

다음 줄에 명령 수 \(Q\)가 주어진다. 이어지는 줄에는 size i 또는 area i가 주어진다.

  • \(1 \le N,Q \le 1,000\)
  • \(1 \le width,height \le 1,000,000,000\)
  • Triangle에서는 \(width \times height\)가 항상 짝수이다.
  • \(1 \le i \le N\)

출력

각 명령마다 반환값과 반환값의 자료형을 출력한다.

예제 입력 1

3
Shape 3 4
Rectangle 3 4
Triangle 3 4
4
area 1
area 2
area 3
size 3

예제 출력 1

0
<class 'int'>
12
<class 'int'>
6
<class 'int'>
(3, 4)
<class 'tuple'>

예제 설명 1

세 객체는 모두 크기 3 4를 저장한다. Shape.get_area()0, Rectangle.get_area()는 \(3 \times 4=12\), Triangle.get_area()는 \(3 \times 4 \div 2=6\)을 반환한다. 삼각형도 부모에게서 상속받은 get_size()를 사용하므로 (3, 4)를 반환한다.

예제 입력 2

1
Rectangle 1 1
2
size 1
area 1

예제 출력 2

(1, 1)
<class 'tuple'>
1
<class 'int'>

코멘트

현재 작성된 코멘트가 없습니다.