Exam exam = new Exam("중간고사");
exam.setScore(); // 국, 영, 수 점수 0~100 사이 랜덤 생성
Student student = new Student("emily");
student.setExam(exam);
student.info();
// 학생이름 : emily
// 중간고사 성적
// 국어 : 50, 영어 : 50, 수학 : 50, 총점 : 150, 평균 : 50.0점
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)); } }
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(); } }
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(); } }