[문제 1] 다음을 만족하는 Student 클래스를 작성하시오.

  • 문자형의 학과와 정수형의 학번을 프로퍼티로 선언 후 생성자를 파라미터로 값을 받는다.
  • getter, setter 정의
  • sayHello 메서드를 통해 다음과 같이 출력
[출력예시]
나는 0000학과 00학번 입니다.
더보기
[풀이]

class Student {
  constructor(department, studentNum) {
    this._department = department;
    this._studentNum = studentNum;
  }

  get department() {
    return this._department;
  }
  set department(department) {
    this._department = department;
  }
  get studentNum() {
    return this._studentNum;
  }
  set studentNum(studentNum) {
    this._studentNum = studentNum;
  }

  sayHello() {
    console.log("나는 %s학과 %d학번 입니다.", this._department, this._studentNum);
  }
};

const s = new Student("0000", 00);
s.sayHello();

↑ 풀이방법 보기

 

[문제 2] 아래 국어, 영어, 수학 점수를 생성자 파라미터로 입력받아서 합계와 평균을 구하는 클래스 Student를 작성하시오.

이름 국어 영어 수학
철수 92 81 82
영희 72 95 100
민혁 80 86 89

sum() 과 avg() 메서드를 활용하여 학생별 합계 점수와 평균점수를 출력하시오. (클래스는 JSON 형식으로 작성)

[출력예시]
철수의 총점은 255점 이고 평균은 85점 입니다.
영희의 총점은 267점 이고 평균은 89점 입니다.
민혁의 총점은 255점 이고 평균은 85점 입니다.
더보기
[풀이]

class Student {
  constructor(kor, eng, math) {
    this._kor = kor;
    this._eng = eng;
    this._math = math;
  }

  sum() {
    return this._kor + this._eng + this._math;
  }
  avg() {
    return this.sum() / 3
  }
};

[ 객체를 여러개 생성해서 풀이 1 ]
const s1 = new Student(92, 81, 82);
const s2 = new Student(72, 95, 100);
const s3 = new Student(80, 86, 89);

console.log(`철수의 총점은 ${s1.sum()}점이고, 평균은 ${s1.avg()}점 입니다.}`);
console.log(`영희의 총점은 ${s2.sum()}점이고, 평균은 ${s2.avg()}점 입니다.}`);
console.log(`민혁의 총점은 ${s3.sum()}점이고, 평균은 ${s3.avg()}점 입니다.}`);

--------------------

[ 2차원 배열을 활용한 반복문 풀이 2 ]
const grade = [
  ["철수", 92, 81, 82],
  ["영희", 72, 95, 100],
  ["민혁", 80, 86, 89]
];

for(const i of grade) {
  const s = new Student(i[1], i[2], i[3]);
  console.log(`${i[0]}의 총점은 ${s.sum()}점이고, 평균은 ${s.avg()}점 입니다.}`);
}

↑ 풀이방법 보기

 

[문제 3] width, height 정보를 getter, setter로 관리하는 Rectangle 클래스를 정의하시오.

이 클래스는 생성자 파라미터가 없으며, 가로가 10, 세로가 5인 경우, getAround() 메서드로 둘레의 길이를 구하고, getArea() 메서드로 넓이를 구하세요.

[출력예시]
둘레의 길이는 30이고, 넓이는 50입니다.
더보기
[풀이]

class Rectangle {
  constructor() {
    this._width = null;
    this._height = null;
  }

  get width() {
    return this._width;
  }
  set width(length) {
    this._width = length;
  }
  get height() {
    return this._height;
  }
  set height(length) {
    this._height = length;
  }

  getAround() {
    return (this._width + this._height) * 2;
  }
  getArea() {
    return this._width * this._height;
  }
};

const r = new Rectangle();
r.width = 10;
r.height = 5;
console.log(`둘레의 길이는 ${r.getAround()}이고, 넓이는 ${r.getArea()}입니다.`);

↑ 풀이방법 보기

 

[문제 4] 다음을 만족하는 Account 클래스를 작성하시오.

  • 문자형의 owner와 정수형의 balance를 프로퍼티로 선언 후 생성자를 파라미터로 값을 받는다.
  • 모든 프로퍼티에 대한 getter, setter 구현
  • 금액을 저축하는 메소드 deposit(), 금액을 인출하는 메소드 withdraw() 를 선언하시오.
  • 인출 상한 금액은 잔액까지로 하며, 인출할 수 없을 때는 "인출 할 수 없음" 출력
  • 시작 값은 0 으로 한다.
[출력예시]
1. 5000원 저축
    OO님의 잔액: 5000원
2. 10000원 인출
    인출 할 수 없음
    OO님의 잔액: 5000원
3. 4000원 인출
    OO님의 잔액: 1000원
더보기
[풀이]

class Account {
  constructor(owner, balance) {
    this._owner = owner;
    this._balance = balance;
  }

  get owner() {
    return this._owner;
  }
  set owner(owner) {
    this._owner = owner;
  }
  get balance() {
    return this._balance;
  }
  set balance(balance) {
    this._balance = balance;
  }

  deposit(amount) {
    return this._balance += amount;
  }
  withdraw(amount) {
    if(this._balance > amount) {
      this._balance -= amount;
    } else {
      console.log("인출 할 수 없음");
    }
    return this._balance;
  }
  output() {
    console.log("%s님의 잔액: %d원", this.owner, this.balance);
  }
};

const money = new Account("OO", 0);

console.group('1. 5000원 저축');
  money.deposit(5000);
  money.output();
console.groupEnd();

console.group('2. 10000원 인출');
  money.withdraw(10000);
  money.output();
console.groupEnd();

console.group('3. 4000원 인출');
  money.withdraw(4000);
  money.output();
console.groupEnd();

↑ 풀이방법 보기

+ Recent posts