메인클래스 ScoreMng.java
package king;
import java.util.Scanner;
public class ScoreMng {
private StudentScore[] ss;
public ScoreMng(int num) {
// TODO Auto-generated constructor stub
ss = new StudentScore[num];
for (int i = 0; i < num; i++) {
ss[i] = new StudentScore();
}
}
public void input() {
Scanner sc = new Scanner(System.in);
String name;
int kor, eng, math;
for (int i = 0; i < ss.length; i++) {
System.out.println("이름입력");
name = sc.next();
ss[i].setName(name);
System.out.println("국어점수");
kor = sc.nextInt();
ss[i].setKor(kor);
System.out.println("영어점수");
eng = sc.nextInt();
ss[i].setEng(eng);
System.out.println("수학점수");
math = sc.nextInt();
ss[i].setMath(math);
}
}
public void operator() {
int sum = 0;
double avg;
for (int i = 0; i < ss.length; i++) {
sum += (ss[i].getKor() + ss[i].getMath() + ss[i].getEng());
ss[i].setSum(sum);
avg = sum / 3.f;
ss[i].setAvg(avg);
}
}
public void disp() {
for (int i = 0; i < ss.length; i++) {
System.out.println("\t" + (i + 1) + "번째 학생");
System.out.println("총점 " + ss[i].getSum() + " 평점 " + ss[i].getAvg());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("학생수 입력");
int num = sc.nextInt();
ScoreMng sm = new ScoreMng(num);
sm.input();
sm.operator();
sm.disp();
}
}
서브클래스 총.평점계산 StudentScore.java
package king;
import java.util.Scanner;
public class StudentScore {
private Name name;
private Subject kor;
private Subject eng;
private Subject math;
private int sum;
private double avg;
public StudentScore() {
name = new Name();
kor = new Subject();
eng = new Subject();
math = new Subject();
}
public void setSum(int sum) {
this.sum = sum;
}
public int getSum() {
return sum;
}
public void setAvg(double avg) {
this.avg = avg;
}
public double getAvg() {
return avg;
}
// 아래부터는 이름과 과목들
public void setName(String name) {
this.name.setName(name);
}
public String getName() {
return name.getName();
}
public void setKor(int score) {
kor.setScore(score);
}
public void setEng(int score) {
eng.setScore(score);
}
public void setMath(int score) {
math.setScore(score);
}
public int getKor() {
return kor.getScore();
}
public int getEng() {
return eng.getScore();
}
public int getMath() {
return math.getScore();
}
}
서브클래스 학생이름만 처리 Name.java
package king;
// 이름처리클래스
class Name {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
서브클래스 과목의 점수 처리 Subject.java
package king;
//과목처리클래스
class Subject {
private int score;
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
}