• 求平均值接口与实现该接口的类


    求平均值接口与实现该接口的类,声明一个Average接口,其中约定求平均值的方法,声明多个类实现Average接口,分别给出求平均值的方法实现,例如,在第一组数值中,算法一

    全部数值相加后求平均值,算法二,去掉一个最高分和一个最低分,再将总分求平均,算法三,求加权平均分的值。

    1,在主函数中声明了三个类,第一个类实现全部算法相加后求平均值。

    2,第二个类实现去掉一个最高分和一个最低分之后求平均值。

    3.第三个类实现求加权平均分的值。

    4,程序运行后产生的结果是32.75,9.25,6.75

    5,程序当中遇到的问题有,就是完不成利用数组存贮数据,以及在实例中利用数组完成构造函数。求大神指导。

    public class fubb {

    public static void main(String[] args) {

    Average b=new add(19,20,25,67);
    System.out.println(b.ave());
    Average c=new addd(17,20,21,15);
    System.out.println(c.ave());
    Average d=new adddd(17,20,21,15);
    System.out.println(d.ave());

    }

    }
    interface Average//声明一个求平均值的接口
    {
    double ave();
    }
    class add implements Average
    {
    int a,b,c,d;
    add(int a,int b,int c,int d)//构造函数
    {
    this.a=a;
    this.b=b;
    this.c=c;
    this.d=d;
    }


    public double ave()
    {
    double sum=0.0;
    sum=this.a+this.b+this.c+this.d;
    return sum/4.0;
    }
    }
    class addd implements Average
    {
    int a,b,c,d;
    addd(int a,int b,int c,int d)//构造函数
    {
    this.a=a;
    this.b=b;
    this.c=c;
    this.d=d;
    }

    public double ave()
    {
    double sum=0.0;
    int max,min;
    max=this.a;
    min=this.a;
    if(this.b>max)
    max=this.b;
    else
    min=this.b;
    if(this.c>max)
    max=this.c;
    else
    min=this.c;
    if(this.d>max)
    max=this.d;
    else
    min=this.d;
    sum=this.a+this.b+this.c+this.d-min-max;
    return sum/4;


    }
    }
    class adddd implements Average
    {
    int a,b,c,d;
    adddd (int a,int b,int c,int d)//构造函数
    {
    this.a=a;
    this.b=b;
    this.c=c;
    this.d=d;
    }
    public double ave()
    {
    double sum=0.0;
    sum=this.a*0.3+this.b*0.3+this.c*0.4+this.d*0.5;
    return sum/4;
    }

    }

  • 相关阅读:
    浅谈网络语音技术(转)
    常用的XMPP服务器
    双码流 主码流子码流(转)
    C++ 程序员必读书目清单
    Error c3876: function call missing argument list; use '' to create a pointer to member
    C#, C++, Java性能对比
    error LNK2001: unresolved external symbol __imp
    error LNK2005: _DllMain@12 already defined in MSVCRTD.lib
    【转】无缝世界网游服务器架构的设计思路
    VS2010生成exe在别的机子上运行提示“丢失MSVCR100D.dll”
  • 原文地址:https://www.cnblogs.com/sdn1229/p/6798711.html
Copyright © 2020-2023  润新知