TIL_180607 다양한 정렬검색을 통한 학생점수 관리 프로그램

Java programming Assignment

자바 프로그래밍 수업 어싸 과제제출로 나왔던 문제 중 하나.
sort()와 같은 api사용 없이 이진/삽입/순차 검색을 통한 정렬법을 사용하여
학생 점수를 관리하는 프로그램이다. 모든 것은 사용자의 입력값을 기반으로 움직임.
사실 실용성(?)보다는 다양한 검색법을 익히기 위한 프로그래밍이다.
저놈의 검색법때문에 헷갈리고 헷갈렸더랜다.


Write a java program to read the name and mark of 3 courses for a number of students (not more than 20).
The program calculates the average of each student and show his/her grade:
The grade is calculated as follows:

Average Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F

The program then shows the following menu and asks the user for his choice:


1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average.
4: Ask the user to enter an average and search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
Enter your choice?

Each time the user chooses an option the program shows the required output and the redisplays the menu again until the user selects 0.


The following has to be used:

  • Selection sort for option 2.
  • Insertion sort for option 3.
  • Binary search for option 4.
  • Sequential search for option 5.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.util.Scanner;
// Student class. Mark is different class type
class Student {
String name;
Mark mark;
// Student Constructor
Student(String name, int mk1, int mk2, int mk3) {
this.name = name;
this.mark = new Mark(mk1, mk2, mk3);
}
}
// Mark constructor
class Mark {
int mk1;
int mk2;
int mk3;
int tot;
int avg;
// Calculate grade
String grade(){
if (avg >= 90 && avg <= 100) {
return "A";
} else if (avg >= 80 && avg <= 89) {
return "B";
} else if (avg >= 70 && avg <= 79) {
return "C";
} else if (avg >= 60 && avg <= 69) {
return "D";
} else {
return "F";
}
};
Mark(int mk1, int mk2, int mk3){
this.mk1 = mk1;
this.mk2 = mk2;
this.mk3 = mk3;
this.tot = mk1 + mk2 + mk3;
this.avg = tot /3;
this.grade();
}
}
class studentManage {
// Print All student's informations.
public static void printStudentInfo(Student[] studentList){
System.out.println("Name\tMark1\tMark2\tMark3\tAvg\tGrade");
for (int i = 0; i < studentList.length; i++) {
System.out.print(studentList[i].name + "\t");
System.out.print(studentList[i].mark.mk1 + "\t");
System.out.print(studentList[i].mark.mk2 + "\t");
System.out.print(studentList[i].mark.mk3 + "\t");
System.out.print(studentList[i].mark.avg + "\t");
System.out.println(studentList[i].mark.grade() + "\t");
}
}
// Print All student's informations in descending order
public static void reversePrintStudentInfo(Student[] studentList){
System.out.println("Name\tMark1\tMark2\tMark3\tAvg\tGrade");
for (int i = studentList.length-1; i >= 0; i--) {
System.out.print(studentList[i].name + "\t");
System.out.print(studentList[i].mark.mk1 + "\t");
System.out.print(studentList[i].mark.mk2 + "\t");
System.out.print(studentList[i].mark.mk3 + "\t");
System.out.print(studentList[i].mark.avg + "\t");
System.out.println(studentList[i].mark.grade() + "\t");
}
}
// Print Menu
public static void showMenu() {
System.out.println("===========================================================");
System.out.println("1: Print the entire list");
System.out.println("2: Sort and print the list alphabetically");
System.out.println("3: Sort and print the list in descending order based on the average");
System.out.println("4: Search for the student who has that average");
System.out.println("5: Find the student who has the minimum average");
System.out.println("6: Print the grade distribution");
System.out.println("0: Exit");
System.out.println("==========================================================");
System.out.print("Enter your choice? ");
}
public static void main(String[] args){
// Make Scanner's instance to get the value from user
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of students ? ");
int numOfStudents = in.nextInt();
// studentList(Array)'s size be decided by user's input
// Make 'student' automatically
Student[] studentList = new Student[numOfStudents];
String name;
int mk1, mk2, mk3;
// To get student's information
for(int i = 0; i < studentList.length; i++){
System.out.print("Enter name and 3 marks for student " + (i+1) +"? ");
name = in.next();
mk1 = in.nextInt();
mk2 = in.nextInt();
mk3 = in.nextInt();
studentList[i] = new Student(name, mk1, mk2, mk3);
}
int sel;
while(true){
showMenu();
sel = in.nextInt();
// Func according to user's input
switch(sel){
// Print entire list
case 1:
printStudentInfo(studentList);
break;
// *Selection Search* Sort and print the list alphabetically
case 2:
for (int i = 0; i < studentList.length; i++) {
// Find the minimum in the student list
Student Min= studentList[i];
int Index = i;
for (int j = i + 1; j < studentList.length; j++) {
if (Min.name.compareTo(studentList[j].name) > 0) {
Min = studentList[j];
Index = j;
}
}
// Swap studentList[i] with studentList[Index]. if necessary
if (Index != i) {
studentList[Index] = studentList[i];
studentList[i] = Min;
}
}
printStudentInfo(studentList);
break;
// *Insertion Search* Sort & print the list in descending order based on the average.
case 3:
for (int i = 1; i < studentList.length; i++) {
Student currentElement = studentList[i];
int k;
for (k = i - 1; k >= 0 && studentList[k].mark.avg > currentElement.mark.avg; k--) {
studentList[k + 1] = studentList[k];
}
// Insert the current element into studentList[k + 1]
studentList[k + 1] = currentElement;
}
reversePrintStudentInfo(studentList);
break;
// *Binary Search* Ask the user to enter an average and search for the student who has that average
case 4:
System.out.print("Enter the average to search:");
int searchAvg = in.nextInt();
int low = 0;
int high = studentList.length - 1;
int mid = (low + high) / 2;
while (studentList[high].mark.avg >= studentList[low].mark.avg) {
// If input value exact saime with mid, Print the result, then Finish
if (searchAvg == studentList[mid].mark.avg){
System.out.println("The student who has that average: "+ studentList[mid].name);
break;}
else if (searchAvg < studentList[mid].mark.avg)
studentList[high] = studentList[mid - 1];
else {
studentList[low] = studentList[mid + 1];}
}
System.out.println("The student who has that average: "+ studentList[mid].name);
break;
// *Sequential Search* Find the student who has the minimum average
case 5:
// initialize min's value as 'student'
Student min = studentList[0];
for (int j = 0; j < studentList.length; j++) {
if (studentList[j].mark.avg < min.mark.avg) {
min = studentList[j];
}
}
System.out.println("Student with min average is: " + min.name + " Average= " + min.mark.avg);
break;
// Print Grade distribution
case 6:
// variables for counting each grade
int cntA = 0;
int cntB = 0;
int cntC = 0;
int cntD = 0;
int cntF = 0;
for(int i = 0; i < studentList.length; i++){
String grade = (studentList[i].mark.grade());
if(grade == "A") cntA += 1;
else if(grade == "B") cntB += 1;
else if(grade == "C") cntC += 1;
else if(grade == "D") cntD += 1;
else { cntF += 1;}
}
System.out.print("Grade distribution: ");
System.out.print(" A=" + cntA);
System.out.print(" B=" + cntB);
System.out.print(" C=" + cntC);
System.out.print(" D=" + cntD);
System.out.println(" F=" + cntF);
break;
// Exit
case 0:
System.out.println("This is the end of the program...Thank you");
return;
}
}
}
}

출력결과

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Enter the number of students ? 5Enter name and 3 marks for student 1? xxx 90 100 90Enter name and 3 marks for student 2? fff 10 20 10
Enter name and 3 marks for student 3? aaa 70 80 90
Enter name and 3 marks for student 4? ccc 30 50 90
Enter name and 3 marks for student 5? eee 70 70 50===========================================================1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average6: Print the grade distribution
0: Exit==========================================================
Enter your choice? 1
Name Mark1 Mark2 Mark3 Avg Grade
xxx 90 100 90 93 A
fff 10 20 10 13 F
aaa 70 80 90 80 B
ccc 30 50 90 56 F
eee 70 70 50 63 D
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 2
Name Mark1 Mark2 Mark3 Avg Grade
aaa 70 80 90 80 B
ccc 30 50 90 56 F
eee 70 70 50 63 D
fff 10 20 10 13 F
xxx 90 100 90 93 A
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 3
Name Mark1 Mark2 Mark3 Avg Grade
xxx 90 100 90 93 A
aaa 70 80 90 80 B
eee 70 70 50 63 D
ccc 30 50 90 56 F
fff 10 20 10 13 F
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 4
Enter the average to search:63
The student who has that average: eee
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 5
Student with min average is: fff Average= 13
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 6
Grade distribution: A=1 B=1 C=0 D=1 F=2
===========================================================
1: Print the entire list
2: Sort and print the list alphabetically
3: Sort and print the list in descending order based on the average
4: Search for the student who has that average
5: Find the student who has the minimum average
6: Print the grade distribution
0: Exit
==========================================================
Enter your choice? 0
This is the end of the program...Thank you

getter setter? or Constructor?

처음에 짤때는 학생값을 세터를 통해 입력하고 게터를 통해 가져오는 방식을 사용했다.
그런데 평균 점수를 가지고 정렬하는게 대부분이었기에, 접근할때에 코드가 무지하게 더러웠다.
사실 어싸 마감기한떄문에 이렇게 마무리를 하긴 해는데. 분명 깔끔하게 만들고자 하면 훠어어얼씬
깔끔하게 만들 수 있는 여지가 엄청나게 많은 못난이 코드를 만듬. 아쉬운 부분이긴 한데,
다음 어싸가 계속 있기때문에 더 손댈 시간이 없다 ㅠㅠㅠ 이놈의 나라는 도대체
뭔 어싸가 했다하면 출력물이 15장이 훅훅 넘어가는지. 이외에도 다른 문제들도 있었는데
비교적 간단해서 적지 않음. 검색법은 어떤 언어에서든 기본소양(?)처럼 이야기 되는 부분이니
다시한번 집고 넘어가면 좋을듯하다. 그나저나 전화번호부도 업데이트해야하는데
학기 중간이 넘어가니 시간이 점차 빠듯하다. 마음같아선 TIL 매일하면 좋겠구만.
그날그날 과제를 쳐내기에 바빠 엄청나게 오랜만에 기록하는 듯 :(