接口与实现:实验1

接口:

package mypackage;

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

接口实现:

package mypackage;

public class Gymnastics implements computerAverage {

	@Override
	public 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 {

	@Override
	public 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));
		}
	}



posted on 2017-11-24 14:52  MACHINE_001  阅读(188)  评论(0编辑  收藏  举报

导航