본문 바로가기
Java_language

간단한 자바 성적 관리 프로그램 (정답)

by 헬로제이콥 2025. 12. 5.

💻 프로그램 구현 (예시 정답)

다음은 위 문제에 대한 예시 코드입니다.

1. Student.java

Java
 
/**
 * Student 클래스: 학생 한 명의 정보를 저장하고 관리합니다.
 */
public class Student {
    // 필드 (멤버 변수) 선언
    private String name;    // 학생 이름
    private int korean;     // 국어 성적
    private int english;    // 영어 성적
    private int math;       // 수학 성적
    private int total;      // 총점 (자동 계산)
    private double average; // 평균 (자동 계산)

    /**
     * 생성자: 이름, 국어, 영어, 수학 성적을 받아 필드를 초기화하고 총점과 평균을 계산합니다.
     * @param name 학생 이름
     * @param korean 국어 성적
     * @param english 영어 성적
     * @param math 수학 성적
     */
    public Student(String name, int korean, int english, int math) {
        // 필드 초기화
        this.name = name;
        this.korean = korean;
        this.english = english;
        this.math = math;

        // 총점 계산 및 저장
        this.total = this.korean + this.english + this.math;

        // 평균 계산 및 저장 (정수 나눗셈 방지를 위해 3.0으로 나눔)
        this.average = this.total / 3.0;
    }

    /**
     * 학생의 모든 성적 정보를 출력하는 메서드
     */
    public void displayStudentInfo() {
        System.out.println("--- 학생 정보 ---");
        System.out.println("이름: " + this.name);
        System.out.println("국어: " + this.korean);
        System.out.println("영어: " + this.english);
        System.out.println("수학: " + this.math);
        System.out.println("총점: " + this.total);
        // 평균은 소수점 첫째 자리까지만 출력하도록 포맷팅
        System.out.printf("평균: %.1f%n", this.average);
        System.out.println("-----------------");
    }
}

2. Main.java (실행 클래스)

Java
 
/**
 * Main 클래스: Student 객체를 생성하고 메서드를 호출하여 프로그램을 실행합니다.
 */
public class Main {
    public static void main(String[] args) {
        // 1. 첫 번째 Student 객체 생성
        // 생성자를 호출하면서 이름, 국어, 영어, 수학 성적을 전달
        Student student1 = new Student("김철수", 90, 85, 92);

        // 2. 두 번째 Student 객체 생성
        Student student2 = new Student("박영희", 75, 88, 70);

        // 3. 세 번째 Student 객체 생성 (선택 사항)
        Student student3 = new Student("이민지", 100, 95, 98);

        System.out.println("### 학생 성적 관리 프로그램 실행 ###");

        // 첫 번째 학생의 정보 출력 메서드 호출
        student1.displayStudentInfo();

        // 두 번째 학생의 정보 출력 메서드 호출
        student2.displayStudentInfo();

        // 세 번째 학생의 정보 출력 메서드 호출
        student3.displayStudentInfo();
    }
}

💡 추가 실습 및 학습 포인트

  • 캡슐화(Encapsulation): Student 클래스의 필드 앞에 private 키워드를 사용하여 외부 접근을 막고, public 메서드를 통해서만 접근하도록 변경해 보세요. (예시 코드에서는 이미 private로 설정되어 있습니다.)

 

 

1️⃣ 학생 성적 관리 프로그램 실행 결과

첫 번째 문제인 Student 클래스를 이용한 성적 관리 프로그램의 Main.java를 실행했을 때의 출력 결과입니다.

### 학생 성적 관리 프로그램 실행 ###
--- 학생 정보 ---
이름: 김철수
국어: 90
영어: 85
수학: 92
총점: 267
평균: 89.0
-----------------
--- 학생 정보 ---
이름: 박영희
국어: 75
영어: 88
수학: 70
총점: 233
평균: 77.7
-----------------
--- 학생 정보 ---
이름: 이민지
국어: 100
영어: 95
수학: 98
총점: 293
평균: 97.7
-----------------