+-------------------+
| StaffRecruit |
+-------------------+
| |
| - scores: double[] |
| |
| + average(): double|
| + max(): double |
| + min(): double |
+-------------------+
^
|
|
+-------------------+
| FinalExam |
+-------------------+
| |
| - scores: double[] |
| |
| + average(): double|
| + max(): double |
| + min(): double |
+-------------------+
interface IAverage {
double average(double[] x);
default double max(double[] x) {
double max = Double.MIN_VALUE;
for (double score : x) {
if (score > max) {
max = score;
}
}
return max;
}
default double min(double[] x) {
double min = Double.MAX_VALUE;
for (double score : x) {
if (score < min) {
min = score;
}
}
return min;
}
}
class StaffRecruit implements IAverage {
private double[] scores;
public StaffRecruit(double[] scores) {
this.scores = scores;
}
@Override
public double average(double[] x) {
// 去掉一个最高分和最低分
double sum = 0;
double min = min(x);
double max = max(x);
for (double score : x) {
if (score != min && score != max) {
sum += score;
}
}
return sum / (x.length - 2);
}
}
class FinalExam implements IAverage {
private double[] scores;
public FinalExam(double[] scores) {
this.scores = scores;
}
@Override
public double average(double[] x) {
double sum = 0;
for (double score : x) {
sum += score;
}
return sum / x.length;
}
}
public class Main {
public static void main(String[] args) {
double[] staffRecruitScores = { 80, 90, 95, 85, 88, 92, 87 };
double[] finalExamScores = { 75, 85, 90, 92, 88, 82, 80, 85, 90, 95, 85, 88, 92, 87, 85, 90, 95, 85, 88, 92 };
StaffRecruit staffRecruit = new StaffRecruit(staffRecruitScores);
FinalExam finalExam = new FinalExam(finalExamScores);
System.out.println("Staff Recruit Average: " + staffRecruit.average(staffRecruitScores));
System.out.println("Final Exam Average: " + finalExam.average(finalExamScores));
}
}