JAVA2009. 11. 21. 13:58

성적표 출력 실습 Java

2009/01/18 15:35

작성자: 베레(lsj403)

// 성적을 나타내는 성적클래스  Score.java
class Score {

     int kor; // 국어
     int eng; // 영어
     int math;  // 수학

     Score(int kor, int eng, int math) {
          this.kor = kor;
          this.eng = eng;
          this.math = math;
     }

      // 세 과목의 합계를 연산해서 반환
      int getTotal() {
           return kor+eng+math;
      }

}//class

-----------------------------------------------------------------------------

// 학생을 나타내는 학생클래스  Student.java
class Student {

     String name; // 이름
     Score score; // 성적

     Student() {
     }

     Student(String name, Score score) {
          this.name = name;
          this.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
Posted by Tiwaz