평가 결과 정보
다음 데이터를 요구사항에 맞게 객체로 만들어 출력하세요
- 시험점수는 0 이상 100 이하의 정수로만 설정이 가능하도록 구현
- 결과 출력 시 총점과 평균, 통과 여부를 확인할 수 있어야 함
- 과목별 40점 이상이고 평균 60점 이상이면 통과, 아니면 재평가
Student 클래스
package day10_oop.getter2;
public class Student {
// 멤버 필드
String name, course;
int score, scoreC;
// name에 대한 getter & setter
String getName() {
return this.name;
}
void setName(String name) {
this.name = name;
}
// course에 대한 getter & setter
String course() {
return this.course;
}
void setCourse(String course) {
this.course = course;
}
// score에 대한 getter & setter
int getScore() {
return this.score;
}
void setScore(int score) {
if(score >= 0 && score <= 100) { // set을 설정하는 방법 1)
this.score = score;
}
}
// scoreC에 대한 getter & setter
int getScoreC() {
return this.scoreC;
}
void setScoreC(int scoreC) {
if(scoreC < 0 && scoreC > 100) { // set을 설정하는 방법 2)
return;
}
this.scoreC = scoreC;
}
// 총합에 대한 getter
int getSum() {
return this.score + this.scoreC;
}
// 평균에 대한 getter
double getAvg() {
return getSum() / 3.0;
}
// 생성자
Student(String name, String course, int score, int scoreC){
this.setName(name);
this.setCourse(course);
this.setScore(score);
this.setScoreC(scoreC);
}
// 출력
void print() {
System.out.println("이름 : " + this.name);
System.out.println("과목 : " + this.course);
System.out.println("서술형 점수 : " + this.score);
System.out.println("평가자 체크리스트 점수 : " + this.scoreC);
System.out.println("총점 : " + getSum());
System.out.println("평균 : " + getAvg());
System.out.print("통과 여부 : ");
if(score >= 40 && scoreC >= 40 && getAvg() >= 60) {
System.out.println("통과");
}
else {
System.out.println("재평가");
}
System.out.println(); // 출력시 구분을 위한
}
}
메인 메소드
package day10_oop.getter2;
public class Test01 {
public static void main(String[] args) {
// 객체 생성
Student a = new Student("피카츄", "응용SW기초기술활용", 50, 60);
Student b = new Student("라이츄", "응용SW기초기술활용", 40, 80);
Student c = new Student("파이리", "프로그래밍언어", 60, 65);
// 출력
a.print();
b.print();
c.print();
}
}
1. setter 메소드 작성법
1) 조건을 만족할 때 값을 반환
void setScore(int score) {
if(score >= 0 && score <= 100) { // set을 설정하는 방법 1)
this.score = score;
}
}
2) 조건에 맞지 않을 경우 return하며 조건을 만족할 때 값을 반환
void setScoreC(int scoreC) {
if(scoreC < 0 && scoreC > 100) { // set을 설정하는 방법 2)
return;
}
this.scoreC = scoreC;
}
** 생성자의 역할
1) 클래스의 인스턴스 생성시 필드값을 초기화한다 (필드값을 기본값으로 만든다)
2) 클래스의 인스턴스 생성시 실행해야 할 작업을 지정할 수 있다
2. 생성자와 setter 메소드
- 클래스의 인스턴스 생성시 반드시 name, course, score, scoreC를 입력해야 한다
- 클래스의 인스턴스 생성시 인스턴스(this)의 setter 메소드(setName, setCourse, setScore, setScoreC)를 호출한다
Student(String name, String course, int score, int scoreC){
this.setName(name);
this.setCourse(course);
this.setScore(score);
this.setScoreC(scoreC);
}
- 메소드 호출 결과로
인스턴스(this)의 멤버필드 name은 입력한 name의 값이, 멤버 필드 course는 입력한 course의 값이 되며
인스턴스(this)의 멤버필드 score는 0 이상 100 이하의 값을 입력한 경우에만 입력한 score의 값이 되며
인스턴스(this)의 멤버필드 scoreC는 0 이상 100 이하의 값을 입력한 경우에만 입력한 scoreC의 값이 된다
void setName(String name) {
this.name = name;
}
void setCourse(String course) {
this.course = course;
}
void setScore(int score) {
if(score >= 0 && score <= 100) {
this.score = score;
}
}
void setScoreC(int scoreC) {
if(scoreC < 0 && scoreC > 100) {
return;
}
this.scoreC = scoreC;
}
'국비교육 > 국비교육 복습' 카테고리의 다른 글
day10_oop.modifier2 : 접근제한을 붙여서 클래스 생성 (0) | 2022.08.13 |
---|---|
day10_oop.getter3 : 멤버필드를 새로 정의하지 않고 총합, 평균 구하기 (2) (0) | 2022.08.13 |
Day10_oop.setter2 : setter의 활용 (★) (0) | 2022.08.07 |
Day10_oop.constructor2 : 생성자 연습 + 오버로딩 (★) (0) | 2022.08.07 |
Day09_oop.method7 : 메소드 연습 (6) (0) | 2022.08.07 |