Exam 클래스
public class Exam { private String examName; private int kor; private int eng; private int mat; public Exam(String examName) { this.examName = examName; } public void setScore() { kor = (int)(Math.random() * 101); eng = (int)(Math.random() * 101); mat = (int)(Math.random() * 101); } public void examInfo() { System.out.println(examName + "성적"); System.out.println("국어:" + kor + ", 영어:" + eng + ", 수학" + mat); int total = kor + eng + mat; System.out.println("총점:" + total + ", 평균:" + (total / 3.0)); }
Student 클래스
public class Student { private String name; private Exam exam; public Student(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Exam getExam() { return exam; } public void setExam(Exam exam) { this.exam = exam; } public void info() { System.out.println("학생명:" + name); exam.examInfo(); }
Main 클래스
public class Main { public static void main(String[] args) { Exam exam = new Exam("중간고사"); exam.setScore(); // 국, 영, 수학 점수 0 ~ 100 사이 랜덤 생성 Student student = new Student("emily"); student.setExam(exam); student.info(); } }
출력
학생명:emily
중간고사성적
국어:31, 영어:82, 수학11
총점:124, 평균:41.333333333333336