歌手大赛计算选手成绩的方法是去掉一个最高分和一个最低分后再计算平均分,而学校评估一个班级的学生成绩的平均分时,是计算全班同学的平均得分。SongGame和School类都实现了ComputeAverage接口,但实现方式不同。 1)编写接口ComputeAverage,声明一个public double average(double x[])方法 2)编写SongGame类,实现ComputeAverage接口 3)编写School类 实现ComputeAverage接口 4)编写主类Application,给定一组10个学生成绩数据a[]和一个歌手的10个得分b[],分别计算学生平均分和歌手最后得分并输出java
时间: 2024-03-23 08:43:16 浏览: 97
1)ComputeAverage接口代码:
```
public interface ComputeAverage {
public double average(double x[]);
}
```
2)SongGame类代码:
```
public class SongGame implements ComputeAverage {
public double average(double x[]) {
double max = x[0];
double min = x[0];
double sum = 0;
for (int i = 0; i < x.length; i++) {
if (x[i] > max) {
max = x[i];
}
if (x[i] < min) {
min = x[i];
}
sum += x[i];
}
return (sum - max - min) / (x.length - 2);
}
}
```
3)School类代码:
```
public class School implements ComputeAverage {
public double average(double x[]) {
double sum = 0;
for (int i = 0; i < x.length; i++) {
sum += x[i];
}
return sum / x.length;
}
}
```
4)主类Application代码:
```
public class Application {
public static void main(String[] args) {
double[] a = {80, 90, 85, 95, 70, 75, 88, 92, 86, 79};
double[] b = {9.5, 9.8, 9.3, 9.7, 9.2, 9.0, 9.6, 9.4, 9.1, 9.9};
ComputeAverage studentAverage = new School();
ComputeAverage singerAverage = new SongGame();
double studentAvg = studentAverage.average(a);
double singerAvg = singerAverage.average(b);
System.out.println("学生成绩平均分:" + studentAvg);
System.out.println("歌手最后得分:" + singerAvg);
}
}
```
输出结果为:
```
学生成绩平均分:84.0
歌手最后得分:9.48
```
阅读全文