학생 성적 정보
평균의 소수점 자리수는 신경쓰지 마세요
- 각 점수는 0점 이상 100점 이하일 경우에만 설정하도록 필터링 처리
- 정보 출력시 반드시 총점, 평균, 등급이 나오도록 구현
- 등급은 다음과 같이 계산합니다
90점 이상 100점 이하 : A
80점 이상 89점 이하 : B
70점 이상 79점 이하 : C
70점 미만 : F
Student 클래스
package day10_oop.getter3;
public class Student {
// 멤버 필드
String name;
int level, korean, english, math;
// name에 대한 getter & setter
String getName() {
return this.name;
}
void setName(String name) {
this.name = name;
}
// level에 대한 getter & setter
int getLevel() {
return this.level;
}
void setLevel(int level) {
switch(level) {
case 1: case 2: case 3:
break;
}
this.level = level;
}
// korean에 대한 getter & setter
int getKorean() {
return this.korean;
}
void setKorean(int korean) {
if(korean >= 0 && korean <= 100) {
this.korean = korean;
}
}
// english에 대한 getter & setter
int getEnglish() {
return this.english;
}
void setEnglish(int english) {
if(english >= 0 && english <= 100) {
this.english = english;
}
}
// math에 대한 getter & setter
int getMath() {
return this.math;
}
void setMath(int math) {
if(math >= 0 && math <= 100) {
this.math = math;
}
}
// 총점 및 평균에 대한 getter
int getSum() {
return this.korean + this.english + this.math;
}
int getAvg() {
return getSum() / 3;
}
// 등급에 대한 getter
String getGrade() {
if(getAvg() < 70) {
return "F";
}
else if(getAvg() < 80) {
return "C";
}
else if(getAvg() < 90) {
return "B";
}
else {
return "A";
}
}
// 생성자
Student(String name, int level, int korean, int english, int math){
this.setName(name);
this.setKorean(korean);
this.setEnglish(english);
this.setMath(math);
}
// 출력 메소드
void print() {
System.out.println("이름 : " + this.name);
System.out.println("총점 : " + this.getSum());
System.out.println("평균 : " + this.getAvg());
System.out.println("등급 : " + this.getGrade());
System.out.println();
}
}
- if문을 사용하여 getter 메소드에서 등급을 반환할 수 있다
메인 메소드
package day10_oop.getter3;
public class Test01 {
public static void main(String[] args) {
// 객체 생성
Student a = new Student("마리오", 1, 90, 80, 70);
Student b = new Student("루이지", 1, 85, 85, 83);
Student c = new Student("쿠파", 3, 70, 60, 55);
// 출력
a.print();
b.print();
c.print();
}
}
'국비교육 > 국비교육 복습' 카테고리의 다른 글
day10_oop.modifier4 : 경찰 객체 생성시 총 객체를 자동으로 소유하도록 (★★) (0) | 2022.08.13 |
---|---|
day10_oop.modifier2 : 접근제한을 붙여서 클래스 생성 (0) | 2022.08.13 |
day10_oop.getter2 : 멤버필드를 새로 정의하지 않고 총합, 평균 구하기 (1) (0) | 2022.08.13 |
Day10_oop.setter2 : setter의 활용 (★) (0) | 2022.08.07 |
Day10_oop.constructor2 : 생성자 연습 + 오버로딩 (★) (0) | 2022.08.07 |