티스토리 뷰
ArrayList를 활용한 성적 평가 예제입니다.
ArrayList에 대해선 따로 포스팅 하겠습니다.
package edu.ks.exam2;
import java.util.ArrayList;
import java.util.Scanner;
public class J0502_3 {
private int jumsu1;
private int jumsu2;
private int jumsu3;
private int jumsu4;
private int jumsu5;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//1
ArrayList<Student> st = new ArrayList<Student>(); // arrraylist 생성 <객체타입> 동적으로 객체를 생성하기 위해
String ans;
String number;
String name;
int total;
int avr=0;
int i =0;
hakjumc hj = new hakjumc();
J0502_3 a = new J0502_3();
do{
System.out.println("성적을 입력할 학생이 있으면 y 없으면 n을 입력하세요.");
ans = scan.next();
if(ans.equals("n")) break;
//2 Student 클래스의 student 객체 생성
Student student= new Student();
//3 객체에 값을 넣음
System.out.println("학번 : ");
student.setBunho(scan.next());
System.out.println("이름 : ");
student.setName(scan.next());
System.out.println("출석점수 : ");
student.setJumsu1(scan.nextInt());
System.out.println("과제점수 : ");
student.setJumsu2(scan.nextInt());
System.out.println("수시시험 : ");
student.setJumsu3(scan.nextInt());
System.out.println("중간고사 : ");
student.setJumsu4(scan.nextInt());
System.out.println("기말고사 : ");
student.setJumsu5(scan.nextInt());
//4 점수의 합 구하기
total = student.getJumsu1()+student.getJumsu2()+student.getJumsu3()+student.getJumsu4()+student.getJumsu5();
student.setTotal(total);
System.out.println("평균 : "+student.getTotal()/5);
hj.setTotal(student.getTotal()/5);
avr+=student.getTotal()/5;
student.setGrade(hj.hakjum());
System.out.println("학점 : "+student.getGrade());//학점계산 클래스로 호출
st.add(student); // student 객체를 student 객체유형의 ArrayList에 넣음
}while(true);
System.out.println("종료");
System.out.println("****성적 평가표****");
System.out.println("학번 \t이름 \t 평균\t 학점");
for(i=0; i<st.size();i++){
System.out.print(st.get(i).getBunho()+"\t"+st.get(i).getName()+"\t"+st.get(i).getTotal()/5+"\t"+st.get(i).getGrade());
System.out.println();
}
System.out.println("평균점수는 "+(double)(avr/st.size())+"입니다.");
}
}
class hakjumc{
private int total;
private String grade;
public void setTotal(int total){
this.total = total;
}
public String hakjum(){
switch(total/10){
case 10:
case 9:
grade = "A";
break;
case 8:
grade = "B";
break;
case 7:
grade = "C";
break;
case 6:
grade = "D";
break;
default : grade = "F";
}
return grade;
}
}
----------main----------
package edu.ks.exam2;
public class Student {
private String bunho;
private String name;
private int jumsu1;
private int jumsu2;
private int jumsu3;
private int jumsu4;
private int jumsu5;
private int total;
private String grade;
public String getBunho() {
return bunho;
}
public void setBunho(String bunho) {
this.bunho = bunho;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getJumsu1() {
return jumsu1;
}
public void setJumsu1(int jumsu1) {
this.jumsu1 = jumsu1;
}
public int getJumsu2() {
return jumsu2;
}
public void setJumsu2(int jumsu2) {
this.jumsu2 = jumsu2;
}
public int getJumsu3() {
return jumsu3;
}
public void setJumsu3(int jumsu3) {
this.jumsu3 = jumsu3;
}
public int getJumsu4() {
return jumsu4;
}
public void setJumsu4(int jumsu4) {
this.jumsu4 = jumsu4;
}
public int getJumsu5() {
return jumsu5;
}
public void setJumsu5(int jumsu5) {
this.jumsu5 = jumsu5;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
'IT > Java' 카테고리의 다른 글
[Java] 상속(Inheritance)이란 ? (0) | 2014.05.11 |
---|---|
[Java] ArrayList란? (2) | 2014.05.07 |
[Java] get, set 메소드를 활용한 성적평가 예제 (0) | 2014.05.07 |
[Java] 이차원 배열을 활용한 성적평가 예제 (0) | 2014.05.07 |
[Java] 클래스를 활용한 성적 평가 예제 (0) | 2014.05.01 |