자바기초) 클래스 활용 퀴즈 : 성적 관리 프로그램 5

박대현·2023년 1월 27일
0

자바 기초 활용

목록 보기
12/22

성적 관리 프로그램
학생 정보를 담을 클래스, 학생점수를 담을 클래스를 구현하고
Main에서 학생 정보, 국어, 영어, 수학, 점수, 총점, 평균을 출력해라.


클래스 조건

학생 점수 클래스
멤버 변수가 국어, 영어, 수학 점수로 구성되며
모든 과목 점수를 출력, 총점 계산, 평균 계산 메소드를 구현되어야 한다.

학생 정보
멤버변수가 학생 번호, 학생 이름, 학생 점수로 구성되며
학생의 모든 정보를 출력하는 메소스를 구현되어야 한다.


접근 방법
1. 학생 점수 클래스를 구현한다.
2. 학생 점수를 멤버 변수로 받는 학생 정보 클래스를 구현한다.
3. Main에 학생 점수 클래스와, 학생 정보 클래스의 인스턴스를 생성하여 학생 정보와 학생 점수를 출력한다.


학생 점수 클래스(Scores.java)

//1. 학생 점수 클래스를 구현한다.
public class Scores {
	// 멤버 변수(필드, 프로퍼티)
	int korean;// 국어 점수
	int english;// 영어 점수
	int math;// 수학 점수

	public Scores() {
	}
	
	//매개변수가 있는 생성자
	public Scores(int korean, int english, int math) {
		this.korean = korean;
		this.english = english;
		this.math = math;
	}

	// 세 과목의 총점을 리턴하는 메소드
	public int calcTotal() {
		return korean + english + math;
	}// end calcTotal()

	// 세 과목의 평균을 리턴하는 메소드
	public double calcAverage() {
		return (double) calcTotal() / 3;
	}
	// 세 과목의 각 점수를 출력하는 메소드
	public void printScores() {
		System.out.println("국어 : " + korean);
		System.out.println("영어 : " + english);
		System.out.println("수학 : " + math);
	}
}

학생 정보 클래스(Student.java)

//2. 학생 점수를 멤버 변수로 받는 학생 정보 클래스를 구현한다.
public class Student {

	int stuNo;// 학생 번호
	String name; // 학생 이름
	Scores scores; // 학생 점수
    //학생 점수 클래스를 활용하기 위해 사용된다.
    
	public Student() {
	}

	public Student(int stuNo, String name, Scores scores) {
		this.stuNo = stuNo;
		this.name = name;
		this.scores = scores;
	}

	// 출력 메소드
	public void displayStudentInfo() {
		System.out.println("--- 학생 점보 ---");
		System.out.println("번호 : " + stuNo);
		System.out.println("이름 : " + name);
		System.out.println("국어 점수 :  " + scores.korean);
		System.out.println("영어 점수 : " + scores.english);
		System.out.println("수학 점수 : " + scores.math);
		System.out.println("총점 : " + scores.calcTotal());
		System.out.println("평균 : " + scores.calcAverage());

	}
}

Main (Main.java)

//3. Main에 학생 점수 클래스와, 학생 정보 클래스의 인스턴스를 생성하여 학생 정보와 학생 점수를 출력한다.
public class Main {
	public static void main(String[] args) {
    	//
    
		//방법 1
     	//먼저 Scores 클래스 인스턴스 생성한 다음 Student 클래스 인스턴스를 생성하여 적용하는 방식
         Scores scores1 = new Scores(50, 60, 70);
		Student student1 = new Student(1111, "또치", scores1);
		// student1의 모든 정보를 출력
        student1.displayStudentInfo();
        // student1의 score에 접근해서 평균만 출력
        System.out.println("총점 : " + stu2.scores.calcAverage();
		
		
		System.out.println("===============");
        
		//방법 2
        //한 번에 인스턴스를 생성하는 방식
        Student stu2 = new Student(2,"둘리", new Scores(30,40,50));
        // student2의 Student의 메소드를 활용해 모든 정보를 출력
        student2.displayStudentInfo();
		// student2의 score의 메소드를 활용해 총점만 출력
		System.out.println("총점 : " + stu2.scores.calcTotal());
		

	} 
} 

0개의 댓글