介面與實現:實驗1
阿新 • • 發佈:2019-02-07
介面:
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)); } }