프로젝트) 학생들 국, 영, 수 점수 입력 후 순위 출력.
자바는 Pakage 구조로 데이터 저장.
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Student {
private String myName;
private int kor;
private int eng;
private int math;
//기본 생성자
public Student(String myName, int kor, int eng, int math) {
this.myName = myName;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String getMyName() {
return myName;
}
public void setMyName(String myName) {
this.myName = myNAme;
}
public int getKor() {
return kor;
}
public void settKor(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 getTotal() {
return (this.kor + this.eng + this.math);
}
//평균을 구하는 메서드
public doble getAvg() {
double avg = (double)this.getTotal() / 3;
BigDecimal dec = new BigDecimal(avg);
return dec.setScale(2, RoundingMode.HALF_UP).doubleValue();
}
}
import java.util.Scanner;
public class StdService {
private Student[] stdArray;
//생성자를 통한 초기화
public StdService() {
this.stdArray = new Student[3];
}
//학생을 생성하는 메서드
public void createStudent() {
Scanner scan = new Scanner(System.in);
System.out.println("====== 학생을 생성합니다. ======");
for(int i = 0 ; i < stdArray.length ; i++) {
System.out.println((i+1) + " 번째 학생 이름 : ");
` String myName = scan.next();
System.out.println((i+1) + " 번째 학생 국어 점수 : ");
` int kor = scan.nextInt();
System.out.println((i+1) + " 번째 학생 영어 점수 : ");
` int eng = scan.nextInt();
System.out.println((i+1) + " 번째 학생 수학 점수 : ");
` int math = scan.nextInt();
//학생 객체를 생성
Student st = new Student(myName, kor, eng, math);
//배열에 저장
stdArray[i] = st;
}
System.out.println(" ===== 학생들을 생성완료했습니다. ===== ");
Scan.close();
}
//학생정보를 출력합니다. --> 성정이 좋은순서로
public void printStudent() {
student temp = null;
for(int i = stdArray.length; i > 0; i--) {
for(int j = 0; j < (i - 1); j++) {
if(stdArray[j].gettotal() < stdArray[j+1].getTotal()) {
temp = stdArray[j+1];
stdArray[j+1] = stdArray[j];
stdArray[j] = temp;
}
}
}
//출력
for(int i = 0; i < stdArray.length; i++) {\
Student st = stdArray[i];
System.out.print("이름 : " + st.getMyName() + ", ");
System.out.print("국어 : " + st.getKor() + ", ");
System.out.print("영어 : " + st.getEng() + ", ");
System.out.print("수학 : " + st.getMath() + ", ");
System.out.print("총점 : " + st.getTotal() + ", ");
System.out.print("평균 : " + st.getAvg() + " \n");
}
}
}
public class StdMAin {
public static void main(String[] args) {
StdService service = new StdService();
//학생 생성;
service.createStudent();
//출력
service.printStudent();
}
}