Exam 클래스의 캡슐화 완성본이다.

public class Exam {
	int kor;
	int eng;
	int math;
	
	public Exam() {
		this(0, 0, 0);
	}
	
	public Exam(int kor, int eng, int math) {
		this.kor= kor;
		this.eng=eng;
		this.math=math;
	}
	
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int total() {
		return kor+eng+math;
	}
	public float avg() {
		return total()/3.0f;
	}
	
}
import java.util.Scanner;

public class ExamList {
	private Exam[] exams;
	private int current;
	
	
	public void printList() {
		this.printList(this.current);
	}

	public void printList(int size) {
		System.out.println("┌──────────────────┐");
		System.out.println("│     성적 출력             │");
		System.out.println("└──────────────────┘");
		System.out.println();
			
		//int size = list.current;
		Exam[] exams = this.exams;
			
		for(int i= 0; i<size; i++ ) {
			Exam exam = exams[i];
			int kor = exam.getKor(); //exam.kor;
			int eng = exam.getEng();
			int math = exam.getMath();
				
			int total = exam.total(); //kor+eng+math; 
			float avg= exam.avg();// total/3.0f;
				
			System.out.printf("국어 :%d\n", kor);
			System.out.printf("영어 :%d\n", eng);
			System.out.printf("수학 :%d\n", math);
			
			System.out.printf("총점 : %3d\n", total);
			System.out.printf("평균 : %6.2f\n", avg);
			System.out.println("────────────────────────");
			}
	}
			
	public void inputList() {
			Scanner scan = new Scanner(System.in);
			System.out.println("┌──────────────────┐");
			System.out.println("│     성적 입력             │");
			System.out.println("└──────────────────┘");
			System.out.println();
			
				int kor, eng, math;
				
					do {
						System.out.printf("국어 :");
						kor = scan.nextInt();
						
						if(kor < 0 || 100 < kor)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(kor<0 || 100 < kor);
					
					do {
						System.out.printf("영어 :");
						eng = scan.nextInt();
						
						if(eng < 0 || 100 < eng)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(eng<0 || 100 < eng);
					
					do {
						System.out.printf("수학 :");
						math = scan.nextInt();
						
						if(math < 0 || 100 < math)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(math<0 || 100 < math);
					
					/*
				 	Exam exam = new Exam();
					exam.setKor(kor); //exam.kor=kor;
					exam.setEng(eng);
					exam.setMath(math);
					*/
					Exam exam1 = new Exam();
					
					Exam exam = new Exam(kor,eng,math);
					
					Exam[] exams = this.exams;
					int size = this.current;
					
					if(exams.length == size) {
						//1. 크기가 5개 정도 더큰 새 배열을 생성하시오
						Exam [] temp = new Exam[exams.length +5];
						//2. 값을 이주시키기
						for(int i=0; i<size;i++)
							temp[i] = exams[i];
						//3. list.exams가 새로만든 temp 배열을 참조하도록 한다.
						this.exams = temp;
												
					}
					this.exams[this.current] = exam;   // 꼭해줘야하는 작업
					this.current++;
		
			
			System.out.println("────────────────────────");
	}

		public ExamList() {
			this.exams = new Exam[3];
			this.current=0; 
		} 

}

참고 : https://youtu.be/edKJbYyUapk

댓글남기기