학생은 국어 과목, 수학 과목을 수강중이며 각 과목의 총점을 계산하는 프로그램을 작성하라

 

Subject

package ch09;

public class Subject {
	
	public String subjectName;
	public int score;
	public int subjectID;

}

Student

package ch09;

public class Student {

	int studentID;
	String studentName;
	Subject korea; //참조 자료형
	Subject math; //참조 자료형
	
	public Student(int id,String name) {
		studentID=id;
		studentName=name;
		korea= new Subject(); //생성을 해줘야 O , 대부분 생성자에서 생성
		math= new Subject(); // 생성을 해줘야 O
		
		
	}
	
	public void setKoreaScore(String name, int score) {
		//main에서 korea. 으로 설정해도 O
		korea.subjectName=name;
		korea.score=score;
	}
	public void setMathScore(String name, int score) {
		math.subjectName=name;
		math.score=score;	
	}
	
	public void showScoreInfo() {
		
		int total=korea.score+math.score;
		System.out.println("Total: "+total);
		
	}
}

TotalScore

package ch09;

public class TotalScore {
	//각 학생의 총점을 구함

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student studentKwak = new Student(10,"kwakseoyeon");
		Student studentLee = new Student (11, "Leeyeonwoo");
		Student studentKim = new Student (12, "Kimseowoo");
		
		studentKwak.setKoreaScore("korea", 90);
		studentKwak.setMathScore("math",80);
		
		studentLee.setKoreaScore("korea", 70);
		studentLee.setMathScore("math",30);
		
		studentKim.setKoreaScore("korea", 20);
		studentKim.setMathScore("math", 100);
		
		int KwakTotal= studentKwak.korea.score+studentKwak.math.score;
		int LeeTotal= studentLee.korea.score+studentLee.math.score;
		int KimTotal= studentKim.korea.score+studentKim.math.score;
		
		System.out.println(studentKwak.studentName+" Total: "+KwakTotal);
		System.out.println(studentLee.studentName+" Total: "+LeeTotal);
		System.out.println(studentKim.studentName+" Total: "+KimTotal);
		
	}

}

+ Recent posts