编写一个ComputerAverage类,类中有一个方法求平均分average(double[] data)。定义Athletes类和Student类,它们都是ComputerAverage的子类,Atheletic类中计算选手的平均成绩的方法是去掉一个最低分,去掉一个最高分,然后求平均分。Student类中计算平均分的方法是直接是总分除以总科目。
时间: 2024-02-28 21:57:19 浏览: 20
好的,这是您的第一个问题。下面是代码实现:
```java
public class ComputerAverage {
public double average(double[] data) {
double sum = 0;
for (double d : data) {
sum += d;
}
return sum / data.length;
}
}
public class Athletes extends ComputerAverage {
@Override
public double average(double[] data) {
Arrays.sort(data);
double sum = 0;
for (int i = 1; i < data.length - 1; i++) {
sum += data[i];
}
return sum / (data.length - 2);
}
}
public class Student extends ComputerAverage {
@Override
public double average(double[] data) {
return super.average(data);
}
}
```
在上面的代码中,我们定义了一个`ComputerAverage`类,其中有一个`average`方法,用于计算给定数据的平均值。然后,我们定义了`Athletes`和`Student`类,它们都是`ComputerAverage`的子类,并覆盖了`average`方法来实现不同的计算方法。
在`Athletes`类中,我们首先对数据进行排序,然后去掉最低分和最高分,计算剩余分数的平均值。在`Student`类中,我们直接调用父类的`average`方法来计算平均值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)