接口与实现:实验1

来源:互联网 发布:淘宝店铺粉丝怎么刷 编辑:程序博客网 时间:2024/05/29 12:51

接口:

package mypackage;public interface computerAverage {public abstract double average(double x[]);}

接口实现:

package mypackage;public class Gymnastics implements computerAverage {@Overridepublic double average(double[] x) {// TODO 自动生成的方法存根double average=0;int k= x.length;for(int i=1; i<k; i++){double temp;if(x[i-1]>x[i]){temp=x[i-1];x[i-1]=x[i];x[i]=temp;}}k--;for(int i=1; i<k; i++){average+=x[i];}k--;if(k > 2)average/=k;else average=0;return average;}}


package mypackage;public class School implements computerAverage {@Overridepublic double average(double[] x) {// TODO 自动生成的方法存根double average=0;int k= x.length;for(int i=0; i<k; i++){average+=x[i];}if(k > 2)average/=k;else average=0;return average;}}


主类:

package Main;import mypackage.*;public class main_class {public static void main(String[] args) {// TODO 自动生成的方法存根double a[] ={ 9.89, 9.88, 9.99, 9.12, 9.69,      9.76, 8.97};double b[] ={ 89, 56, 78, 90, 100, 77, 56, 45,      36, 79, 98};computerAverage k=new School();System.out.println("学校平均分:"+k.average(a));k=new Gymnastics();System.out.println("选手平均分:"+k.average(b));}}


阅读全文
0 0
原创粉丝点击