학생별 평균을 구하는 프로그램을 배열을 이용해서 만들려고 하는데
실행 단계에서 자꾸 오류가 나네요 ㅠㅠ
배열안에 객체를 안집어넣어서 생기는 오류 같은데... 뭐가 문젠건지...ㅠㅠ
class Subject {
private String subjectName;
private int score;
public Subject(String subjectName, int score) {
setSubjectName(subjectName);
setScore(score);
}
public String getSubjectName() {
return subjectName;
}
public int getScore() {
return score;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
String info = subjectName + " : " + score + "\n";
return info;
}
}
class Student {
private String studentName;
private Subject[] subjects;
public Student(String studentName, Subject... subejcts) {
setStudentName(studentName);
setSubjects(subjects);
}
public String getStudentName() {
return studentName;
}
public Subject[] getSubjects() {
return subjects;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setSubjects(Subject... subjects) {
this.subjects = subjects;
}
@Override
public String toString() {
String info = "이름 : " + studentName + "\n";
for(Subject temp : subjects) {
info += temp.toString() + "\n";
}
return info;
}
}
class Class {
private int classNum;
private Student[] students;
public Class(int classNum, Student... students) {
setClassNum(classNum);
setStudents(students);
}
public int getClassNum() {
return classNum;
}
public Student[] getStudents() {
return students;
}
public void setClassNum(int classNum) {
this.classNum = classNum;
}
public void setStudents(Student... students) {
this.students = students;
}
@Override
public String toString() {
String info = "<< " + classNum + "반" + " >>";
for(Student temp : students) {
info += temp.toString() + "\n";
}
return info;
}
}
class ArrayEx {
public static void main(String[] args) {
Subject s1 = new Subject("국어", 80);
Subject s2 = new Subject("수학", 60);
Subject s3 = new Subject("영어", 40);
Student a = new Student("민수", s1, s2, s3);
Subject s4 = new Subject("국어", 80);
Subject s5 = new Subject("수학", 80);
Subject s6 = new Subject("영어", 80);
Student b = new Student("철수", s4, s5, s6);
Class c = new Class(1, a, b);
System.out.println(c);
}
}