package j2.lesson01_2;
public class ExamRecordAction4 {
public static void main(String[] args) {
ExamRecord[] rec = new ExamRecord[4];
rec[0] = createExamRecord("Taro", 50, 90, 100);
rec[1] = createExamRecord("Hanako", 70, 30, 80);
rec[2] = createExamRecord("Kenji", 60, 50, 70);
rec[3] = createExamRecord("Aya", 40, 100, 90);
for (int i = 0; i < rec.length; i++) {
showExamRecord(rec[i]);
}
System.out.println("全員の数学の平均点 = " + averageMath(rec));
System.out.println("全員の国語の平均点 = " + averageJapanese(rec));
System.out.println("全員の英語の平均点 = " + averageEnglish(rec));
}
public static ExamRecord createExamRecord(String name,
int math, int japanese, int english) {
ExamRecord r = new ExamRecord();
r.name = name;
r.math = math;
r.japanese = japanese;
r.english = english;
return r;
}
public static void showExamRecord(ExamRecord r) {
System.out.println(r.name + ", 数学 = " + r.math
+ ", 国語 = " + r.japanese
+ ", 英語 = " + r.english
+ ", 3科目の平均 = "
+ ((r.math + r.japanese + r.english) / 3.0));
}
public static double averageMath(ExamRecord[] ra){
int sum = 0;
for(int i=0; i<ra.length; i++){
sum += ra[i].math;
}
return (double)sum / ra.length;
}
public static double averageJapanese(ExamRecord[] ra){
int sum = 0;
for(int i=0; i<ra.length; i++){
sum += ra[i].japanese;
}
return (double)sum / ra.length;
}
public static double averageEnglish(ExamRecord[] ra){
int sum = 0;
for(int i=0; i<ra.length; i++){
sum += ra[i].english;
}
return (double)sum / ra.length;
}
}
|