<<--B站..........欢迎来到DGX的博客..........GitHub-->>

我的B站

JAVA第七次上机实验(接口与实现-计算成绩)

目的:

本实验的目的是让学生掌握类怎样实现接口。

要求:

跳水比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考查一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Diving类和School类都实现了ComputerAverage接口,但实现的方式不同。部分代码如下:

interface CompurerAverage{   //接口ComputerAverage的代码

public double average(double x[]);

}

//Diving类都实现了ComputerAverage接口

class Diving implements CompurerAverage{ ......}

//School类都实现了ComputerAverage接口

class School implements CompurerAverage { ......}

 

 public class E{    //主类

public static void main(String[] args) {

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};

       ......

}

 

package com.test07;
interface ComputerAverage{   //接口ComputerAverage的代码
	public double average(double x[]);
		}
//Diving类都实现了ComputerAverage接口
class Diving implements ComputerAverage{
	public double average(double x[]){
		double ans = 0;
		double ans2 = 0;
		double maxtemp;
		double mintemp;
		maxtemp = x[0];
		mintemp = x[0];
		for (int i = 0;i<x.length;i++){
			
			if(x[i]>maxtemp){
				maxtemp = x[i];	
				
			}
			
			if(x[i]<mintemp){
				mintemp = x[i];	
			
				
			}
			ans = ans+x[i];


		}
		ans2 = (ans-(maxtemp+mintemp))/(x.length-2);
		return ans2;

		
	}
	
}
//School类都实现了ComputerAverage接口
class School implements ComputerAverage {
	public double average(double x[]){
		double ans = 0;
		for(int i=0;i<x.length;i++){
			ans = ans +x[i];

		}	
		double ans2 = ans/x.length;
		return ans2;

	}
	
}

 public class Test07{    //主类
	public static void main(String[] args) {
		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};
		Diving diving = new Diving();
		School school = new School();
		System.out.println("diving average:"+diving.average(a));
		System.out.println("school average:"+school.average(b));

}
}

  

 

posted @ 2021-04-14 15:12  DG息  阅读(967)  评论(0编辑  收藏  举报