[TIL]클래스의 활용
지금까지 다뤘던 클래스 내용을 토대로 간단한 데이터를 입력하여 이를 저장하고 계산하는 프로그램을 만들었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package score1;
import java.util.Scanner;
public class Score {
private Scanner sc = new Scanner(System.in);
private int cnt;// 필드는 초기화하지 않으면 초기값 0
private ScoreVO[] list = new ScoreVO[50];
public void input() {
if(cnt>=50) {
System.out.println("데이터를 더이상 입력할 수 없습니다.");
return;
}
System.out.println("\n데이터 입력...");
// ScoreVO vo = new ScoreVO(); // vo 객체를 생성한다. -- 객체 메모리할당 방법 1)
// System.out.print("이름?");
// vo.setName(sc.next());
//
// System.out.print("국어?");
// vo.setKor(sc.nextInt());
// System.out.print("영어?");
// vo.setEng(sc.nextInt());
// System.out.print("수학?");
// vo.setMath(sc.nextInt());
//
// list[cnt] =vo;
//데이터가 있는 주소값을 리스트에 저장해놓는다.
//이유는 해당 객체는 메소드가 종료되면 사라지기 때문
list[cnt]=new ScoreVO();//객체 메모리할당 방법 2)
System.out.print("이름?");
list[cnt].setName(sc.next());
System.out.print("국어?");
list[cnt].setKor(sc.nextInt());
System.out.print("영어?");
list[cnt].setEng(sc.nextInt());
System.out.print("수학?");
list[cnt].setMath(sc.nextInt());
// list[cnt].setName(s); //NullPointerException 오류 발생
cnt++;
}
public void print() {
System.out.println("\n데이터 출력...");
System.out.println("전체인원수:"+cnt);
for(int i=0;i<cnt;i++) {
System.out.print(list[i].getName()+"\t");
System.out.print(list[i].getKor()+"\t");
System.out.print(list[i].getEng()+"\t");
System.out.print(list[i].getMath()+"\t");
System.out.print(list[i].getTot()+"\t");
System.out.print(list[i].getAve()+"\n");
}
}
public void search() {
System.out.println("\n데이터 검색...");
}
}
|
cs |
VO 클래스 필드값에 이름 국어 영어 수학을 정의하고 이들 데이터를 입력하고 출력하는 메소드를 만들었다.
이때 alt + shift + S + R 을 눌러 일괄로 생성할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package score1;
public class ScoreVO { //VO면 한데이터를 저장함
private String name;
private int kor;
private int eng;
private int math;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getTot() {
return kor+eng+math;
}
public int getAve() {
return getTot()/3;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package score1;
import java.util.Scanner;
public class ScoreApp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ch;
Score ss = new Score();
while (true) {
do {
System.out.print("1.입력 2.출력 3.검색 4.종료 =>");
ch = sc.nextInt();
} while (ch < 1 || ch > 4);
if (ch == 4) {
break;
}
switch (ch) {
case 1:
ss.input();
break;
case 2:
ss.print();
break;
case 3:
ss.search();
break;
}
}
sc.close();
}
}
|
cs |
'Language > JAVA' 카테고리의 다른 글
[TIL] 재귀 호출 (0) | 2020.08.04 |
---|---|
[TIL] 메소드 오버로딩 (0) | 2020.08.03 |
[TIL]메소드 (0) | 2020.07.31 |
[TIL] 참조 변수 (0) | 2020.07.31 |
[TIL] 클래스 (0) | 2020.07.31 |
댓글