// 성적을 나타내는 성적클래스 Score.java int kor; // 국어 Score(int kor, int eng, int math) { // 세 과목의 합계를 연산해서 반환 }//class ----------------------------------------------------------------------------- // 학생을 나타내는 학생클래스 Student.java String name; // 이름 Student() { Student(String name, Score score) { }//class ------------------------------------------------------------------------------ // 메인클래스 StudentMain.java import java.util.ArrayList; class StudentMain { public static void main(String[] args) { // 학생 객체 생성 /* 방법1 Student student1 = new Student("홍길동", new Score(100,90,80)); Student student2 = new Student("강감찬", new Score(90,80,70)); Student student3 = new Student("이순신", new Score(80,90,80)); printResult(student1); printResult(student2); printResult(student3); */ /* 방법2 Student[] sArray = new Student[3]; for (int i=0; i sArray[i] = new Student(); } sArray[0].name = "홍길동"; sArray[0].score = new Score(100,90,80); sArray[1].name = "강감찬"; sArray[1].score = new Score(90,80,70); sArray[2].name = "이순신"; sArray[2].score = new Score(80,90,80); for (int i=0; i printResult(sArray[i]); } */ /* 방법3 */ ArrayList list = new ArrayList(); list.add(new Student("홍길동", new Score(100,90,80))); list.add(new Student("강감찬", new Score(90,80,70))); list.add(new Student("이순신", new Score(80,90,80))); for (int i=0; i printResult((Student)list.get(i)); } } // main static void printResult(Student s) { //홍길동 학생은 국어 100점, 영어 90점, 수학 80점, 총 270점 System.out.println(s.name+" 학생은 국어 " +s.score.kor+"점, 영어 "+s.score.eng+"점, 수학 " +s.score.math+"점, 총 "+s.score.getTotal()+"점"); } } // class
|
'JAVA' 카테고리의 다른 글
enum 열거 상수 (0) | 2009.11.21 |
---|---|
실습5 패키지 컴파일 cmd창 (0) | 2009.11.21 |
점과 점 사이의 거리 출력 실습 (0) | 2009.11.21 |
간단한 성적 출력 (0) | 2009.11.21 |
Random을 이용한 난수 발생 (0) | 2009.11.21 |