[SWEA / 파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향
Algorithm

[SWEA / 파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향

6203. 점수 총점

6203. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 1

 

문제

다음의 결과와 같이 국어, 영어, 수학 점수를 입력받아 합계를 구하는 객체지향 코드를 작성하십시오.
이 때 학생 클래스의 객체는 객체 생성 시 국어, 영어, 수학 점수를 저장하며, 총점을 구하는 메서드를 제공합니다.

(출처 : https://swexpertacademy.com/)

 

입력

89, 90, 100

 

출력

국어, 영어, 수학의 총점: 279

class Student:
    def __init__(self, kor, eng, math):
        self.__kor = kor
        self.__eng = eng
        self.__math = math
    
    @property
    def kor(self):
        return self.__kor
    
    @property
    def eng(self):
        return self.__eng
    
    @property
    def math(self):
        return self.__math
    
    def grade_sum(self):
        return "국어, 영어, 수학의 총점: {0}".format(self.__kor + self.__eng + self.__math)

data = list(map(int, input().split(', ')))
student_data = Student(data[0], data[1], data[2])
print(student_data.grade_sum())

 


6208. 국적

6208. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 2

 

문제

국적을 출력하는 printNationality 정적 메서드를 갖는 Korean 클래스를 정의하고

메서드를 호출하는 코드를 작성해봅시다.

(출처 : https://swexpertacademy.com/)

 

출력

대한민국

대한민국

class Korean:
    @staticmethod
    def printNationality():
        return "대한민국"


print(Korean.printNationality())
print(Korean.printNationality())

 


6217. Graduate Student

6217. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 3

 

문제

name 프로퍼티를 가진 Student를 부모 클래스로 major 프로퍼티를 가진
GraduateStudent 자식 클래스를 정의하고 이 클래스의 객체를
다음과 같이 문자열로 출력하는 코드를 작성하십시오.

(출처 : https://swexpertacademy.com/)

 

출력

이름: 홍길동

이름: 이순신, 전공: 컴퓨터

 

class Student:
    def __init__(self, name):
        self.__name = name
    
    @property
    def name(self):
        return self.__name
    
    def __repr__(self):
        return '이름: {0}'.format(self.name)

class GraduateStudent(Student):
    def __init__(self, name, major):
        super().__init__(name)
        self.__major = major

    @property
    def major(self):
        return self.__major

    def __repr__(self):
        return super().__repr__() + ', 전공: {0}'.format(self.major)

Hong = Student('홍길동')
Lee = GraduateStudent('이순신', '컴퓨터')
print(Hong)
print(Lee)

 


6223. 원의 면적

6223. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 4

 

문제

반지름 정보를 갖고, 원의 면적을 계산하는 메서드를 갖는 Circle 클래스를 정의하고,

생성한 객체의 원의 면적을 출력하는 프로그램을 작성하십시오.

(출처 : https://swexpertacademy.com/)

 

출력

원의 면적: 12.56

 

class Circle:
    def __init__(self, radius):
        self.__radius = radius

    @property
    def radius(self):
        return self.__radius
    
    def area(self):
        return 3.14 * (self.radius) ** 2

cir = Circle(2)
print("원의 면적: {}".format(cir.area()))

 


6225. 사각형의 면적

6225. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 5

 

문제

가로, 세로 정보을 갖고, 사각형의 면적을 계산하는 메서드를 갖는 Rectangle 클래스를 정의하고,

생성한 객체의 사각형의 면적을 출력하는 프로그램을 작성하십시오.

(출처 : https://swexpertacademy.com/)

 

출력

사각형의 면적: 20

 

class Rectangle:
    def __init__(self, width, height):
        self.__width = width
        self.__height = height
    
    @property
    def width(self):
        return self.__width

    @property
    def height(self):
        return self.__height
    
    def area(self):
        return self.width * self.height

rec = Rectangle(5, 4)
print("사각형의 면적: {}".format(rec.area()))

 


6228. 면적 오버라이딩

6228. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 6

 

문제

Shape를 부모 클래스로 Square 자식 클래스를 정의하는 코드를 작성하십시오.

Square 클래스는 length 필드를 가지며, 0을 반환하는 Shape 클래스의 area 메서드를

length * length 값을 반환하는 메서드로 오버라이딩합니다.

(출처 : https://swexpertacademy.com/)

 

출력

정사각형의 면적: 9

 

class Shape:

    def area(self):
        return 0

class Square(Shape):

    def __init__(self, length):
        self.__length = length

    @property
    def length(self):
        return self.__length
    
    def area(self):
        return "정사각형의 면적: {0}".format(self.length ** 2)

sq = Square(3)
print(sq.area())

 


6229. 성별 오버라이딩

6229. [파이썬 프로그래밍 기초(2) 파이썬의 기본 응용] 5. 객체지향 7

 

문제

Person를 부모 클래스로 Male, Female 자식 클래스를 정의하는 코드를 작성하십시오.

"Unknown"을 반환하는 Person 클래스의 getGender 메서드를 Male 클래스와 Female 클래스는

"Male", "Female" 값을 반환하는 메서드로 오버라이딩합니다.

(출처 : https://swexpertacademy.com/)

 

출력

Male

Female

 

class Person:

    def getGender(self):
        return "Unknown"

class Male(Person):

    def getGender(self):
        return "Male"

class Female(Person):

    def getGender(self):
        return "Female"

m = Male()
print(m.getGender())

f = Female()
print(f.getGender())