https://www.youtube.com/watch?v=E2FHXYr61IU&list=PLq8wAnVUcTFX4E2NplMvJfqlcgAeF_BxK&index=4
함수 중심의 처리가아닌 객체중심의 처리가 더 편하다?
인스턴스를 통해서 호출되고 인스턴스를 통해서 데이터를 넘겨받는 형식
package Exam_1.인스턴스_메소드구현;
import java.util.Scanner;
public class Exam_1 {
public static void main(String[] args) {
ExamList list = new ExamList();
//ExamList.init();
list.init();
int menu;
boolean keepLoop = true;
while(keepLoop) {
menu = inputMenu();
switch(menu) {
case 1:
//ExamList.inputList(list);
list.inputList();
break;
case 2:
//ExamList.printList(list);
list.printList();
break;
case 3:
System.out.println("bye~");
keepLoop = false;
break;
default:
System.out.println("잘못된 값을 입력하셨습니다.");
}
}
}
private static int inputMenu() {
Scanner scan = new Scanner(System.in);
int num;
System.out.println("--------------------");
System.out.println("메인메뉴");
System.out.println("메뉴를 선택해주세요");
System.out.println("\t1. 성적입력");
System.out.println("\t2. 성적출력");
System.out.println("\t3. 나가기");
System.out.print("\t>>>");
num = scan.nextInt();
if(num<1||3<num) {
System.out.println("1~3의 숫자만 입력해주세요");
}
return num;
}
}
package Exam_1.인스턴스_메소드구현;
import java.util.Scanner;
public class ExamList {
public int current;
public Exam[] exams;
void inputList() { //앞에 private 있으면 호출 안됨 다른 클래스에서 못씀
Scanner scan = new Scanner(System.in);
System.out.println("--------------------");
System.out.println("성적입력");
int kor, eng, math;
do {
System.out.print("국어 : ");
kor = scan.nextInt();
if(kor<0||100<kor) {
System.out.print("국어성적은 0~100까지 범위만 입력해주세요");
}
}while(kor<0||100<kor);
do {
System.out.print("영어 : ");
eng = scan.nextInt();
if(eng<0||100<eng) {
System.out.println("영어성적은 0~100까지 범위만 입력해주세요");
}
}while(eng<0||100<eng);
do {
System.out.print("수학 : ");
math = scan.nextInt();
if(math<0||100<math) {
System.out.println("수학성적은 0~100까지 범위만 입력해주세요");
}
}while(math<0||100<math);
Exam exam = new Exam();
exam.kor = kor;
exam.eng = eng;
exam.math = math;
Exam[] exams = this.exams;
int size = this.current;
if(exams.length == size) {
//1. 크기가 5개정도 더 큰 새로운 배열 생성
Exam[] temp = new Exam[size+5];
//2. 값을 이주시키기
for(int i=0; i<size; i++) {
temp[i] = exams[i];
//3. list.exams가 새로만든 temp 배열을 참조하도록함
//exams = temp 하고는 다름 이거는 틀림
this.exams = temp;
}
}
this.exams[this.current] = exam; // 이부분이 좀 이해가 안되지만 값이 여기서 들어간듯함
this.current++;
}
void printList() {//overloading
this.printList(this.current);//성적입력할때마다 list.current가 ++됨, 갯수만큼 출력
}
void printList(int size) { //size를 지정하면 size만큼만 출력
System.out.println("---------------------");
System.out.println("성적출력");
//int size = list.current;//성적입력할때마다 list.current가 ++됨, 갯수만큼 출력
Exam[] exams = this.exams;
for(int i=0; i<size; i++) {
Exam exam = exams[i];
int kor = exam.kor;
int eng = exam.eng;
int math = exam.math;
int total = kor+eng+math;
float avg = total / 3.0f;
System.out.printf("국어%d : %d\n", i+1, kor);
System.out.printf("영어%d : %d\n", i+1, eng);
System.out.printf("수학%d : %d\n", i+1, math);
System.out.printf("총점 : %3d\n", total);
System.out.printf("평균 : %6.2f\n", avg);
System.out.println("----------------------");
}
}
public void init() {
this.exams = new Exam[3];
this.current = 0;
}
}
package Exam_1.인스턴스_메소드구현;
public class Exam {
int kor;
int eng;
int math;
}
'Java 코드연습' 카테고리의 다른 글
[뉴렉처] 생성자(Constructor) (0) | 2021.10.21 |
---|---|
[뉴렉처] 캡슐의 은닉성과 접근 지정자 (0) | 2021.10.21 |
[뉴렉처] 함수들을 캡슐화하기 (0) | 2021.10.21 |
[뉴렉처] 코드 실행과 함수 호출 스택 (0) | 2021.10.19 |
[뉴렉처] 함수 오버로딩(overloading) (0) | 2021.10.19 |