欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java上机实践六 接口与实现 实验1 评价成绩

程序员文章站 2022-04-15 18:32:56
interface ComputerAverage{ public double average(double x[]);}class Gymnastics implements ComputerAverage{ public double average(double[] x) { int count = x.length; double aver = 0, temp = 0; for(int i = 0; i < c....
interface ComputerAverage
{
    public double average(double x[]);
}
class Gymnastics implements ComputerAverage
{
    public double average(double[] x)
    {
        int count = x.length;
        double aver = 0, temp = 0;
        for(int i = 0; i < count; i++)
        {
            for(int j = i; j < count; j++)
            {
                if(x[j] < x[i])
                {
                    temp = x[j];
                    x[j] = x[i];
                    x[i] = temp;
                }
            }
        }
        for(int i = 1; i < count-1 ; i++)
        {
            aver += x[i];
        }
        if(count > 2)
        {
            aver = aver / (count - 2);
        }
        else
            aver = 0;
        return aver;
    }
}
class School implements ComputerAverage
{
    @Override
    public double average(double[] x)
    {
        int count = x.length;
        double aver = 0.0,sum = 0.0;
        for(int i = 0; i < count; i++)
        {
            sum += x[i];
        }
        aver = sum/(double)count;
        return aver;
    }
}

public class Estimator
{
    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 };
        ComputerAverage computer;
        computer = new Gymnastics();
        double result = computer.average(a);
        System.out.println("体操选手最后得分: %5.3f\n" + result);
        computer = new School();
        result = computer.average(b);
        System.out.println("班级考试平均分输: %-5.2f\n" + result);
    }
}

 

本文地址:https://blog.csdn.net/zhangnuanxin520/article/details/109643398