您好,欢迎访问三七文档
华北电力大学实验报告||实验名称接口与实现接口的类课程名称Java程序设计||专业班级:信管1301学生姓名:王雯敏学号:201306040121成绩:指导教师:张学斌实验日期:2015.4.20华北电力大学实验报告第2页共15页一、实验目的和要求1理解接口的作用,理解接口和实现接口的类的关系2掌握声明接口,一个类实现多个接口的声明和使用方法3理解内嵌类型的概念,掌握声明内部类的方法二、实验环境Windows2000/WindowsXP,JDK1.2~1.6三、实验内容和步骤实验1评价成绩1.实验要求体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。Gymmastics类和School类都实现了ComputerAverage接口,但实现的方式不同。2.程序模板Estimatior.javainterfaceCompurerAverage{publicdoubleaverage(doublex[]);}classGymnasticsimplementsCompurerAverage{publicdoubleaverage(doublex[]){intcount=x.length;doubleaver=0,temp=0;for(inti=0;icount;i++){for(intj=i;jcount;j++){if(x[j]x[i]){temp=x[j];x[j]=x[i];x[i]=temp;}}}for(inti=1;icount-1;i++){aver=aver+x[i];}if(count2)aver=aver/(count-2);elseaver=0;华北电力大学实验报告第3页共15页returnaver;}}classSchoolimplementsCompurerAverage{//重写publicdoubleaverage(doublex[]);返回数组x[]的元素的算术平均【代码1】//重写publicdoubleaverage(doublex[])方法,返回数组x[]的元素的算术平均}publicclassEstimator{publicstaticvoidmain(Stringargs[]){doublea[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97};doubleb[]={89,56,78,90,100,77,56,45,36,79,98};CompurerAveragecomputer;computer=newGymnastics();doubleresult=【代码2】//computer调用average(doublex[])方法,将数组a传递给参数xSystem.out.printf(%n);System.out.printf(体操选手最后得分:%5.3f\n,result);computer=newSchool();result=【代码3】//computer调用average(doublex[])方法,将数组b传递给参数xSystem.out.printf(班级考试平均分数:%-5.2f,result);}}实验结果【代码1】publicdoubleaverage(doublex[]){doubleaver=0;for(inti=0;ix.length;i++)aver+=x[i];aver/=x.length;returnaver;}【代码2】computer.average(a);【代码3】computer.average(b);华北电力大学实验报告第4页共15页3.实验指导可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口方法。接口产生的多态就是指不同类在实现同一个接口时可能具有不同的实现方式。实验2货车的装载量1.实验要求货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。要求有一个ComputerWeight接口,该接口中有一个方法:publicdoublecomputerWeight()有三个实现该接口的类:Television、Computer和WashMachine。这三个类通过实现接口computerTotalSales给出自重。有一个Truck类,该类用ComputerWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Truck对象所装载的货物的总重量。2.程序模板CheckCarWeight.javainterfaceComputerWeight{publicdoublecomputeWeight();}classTelevisionimplementsComputerWeight{【代码1】//重写computeWeight()方法}classComputerimplementsComputerWeight{【代码2】//重写computeWeight()方法}classWashMachineimplementsComputerWeight{【代码3】//重写computeWeight()方法}classTruck{ComputerWeight[]goods;doubletotalWeights=0;Truck(ComputerWeight[]goods){this.goods=goods;}publicvoidsetGoods(ComputerWeight[]goods){this.goods=goods;华北电力大学实验报告第5页共15页}publicdoublegetTotalWeights(){totalWeights=0;【代码4】//计算totalWeightsreturntotalWeights;}}publicclassCheckCarWeight{publicstaticvoidmain(Stringargs[]){ComputerWeight[]goods=newComputerWeight[650];//650件货物for(inti=0;igoods.length;i++){//简单分成三类if(i%3==0)goods[i]=newTelevision();elseif(i%3==1)goods[i]=newComputer();elseif(i%3==2)goods[i]=newWashMachine();}Trucktruck=newTruck(goods);System.out.printf(\n货车装载的货物重量:%-8.5fkg\n,truck.getTotalWeights());goods=newComputerWeight[68];//68件货物for(inti=0;igoods.length;i++){//简单分成两类if(i%2==0)goods[i]=newTelevision();elsegoods[i]=newWashMachine();}truck.setGoods(goods);System.out.printf(货车装载的货物重量:%-8.5fkg\n,truck.getTotalWeights());}}实验结果【代码1】publicdoublecomputeWeight(){return40.0;}【代码2】publicdoublecomputeWeight(){return50.0;}【代码3】publicdoublecomputeWeight(){return60.0;}【代码4】华北电力大学实验报告第6页共15页for(inti=0;igoods.length;i++)totalWeights=totalWeights+goods[i].computeWeight();3.实验指导对于【代码1】,可以简单返回一个值表示货物的重量,比如:publicdoublecomputerWeight(){returen3.5}对于数组goods的每个单元存放的是实现ComputerWeight接口的对象的引用,实验中的【代码4】通过循环语句让数组的每个单元调用computerWeight()方法,并将该方法返回的值累加到totalWeight,如下所示:for(inti=0;igoods.length;i++){totalWeight+=goods[i].computerWeight();}4.扩展练习在实验的基础上编写一个实现ComputerWeight接口的类,比如Refrigerator,这样一来,货车装载的货物中就可以有Refrigerator类型的对象。当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?不需要修改代码:interfaceComputerWeight{publicdoublecomputeWeight();}classTelevisionimplementsComputerWeight{publicdoublecomputeWeight(){return40.0;}//重写computeWeight()方法}classComputerimplementsComputerWeight{publicdoublecomputeWeight(){return50.0;}//重写computeWeight()方法}classWashMachineimplementsComputerWeight{publicdoublecomputeWeight(){华北电力大学实验报告第7页共15页return60.0;}//重写computeWeight()方法}classRefrigeratorimplementsComputerWeight{publicdoublecomputeWeight(){return45.0;}//重写computeWeight()方法}classTruck{ComputerWeight[]goods;doubletotalWeights=0;Truck(ComputerWeight[]goods){this.goods=goods;}publicvoidsetGoods(ComputerWeight[]goods){this.goods=goods;}publicdoublegetTotalWeights(){totalWeights=0;for(inti=0;igoods.length;i++)totalWeights=totalWeights+goods[i].computeWeight();//计算totalWeightsreturntotalWeights;}}publicclassCheckCarWeight{publicstaticvoidmain(Stringargs[]){ComputerWeight[]goods=newComputerWeight[650];//650件货物for(inti=0;igoods.length;i++){//简单分成三类华北电力大学实验报告第8页共15页if(i%4==0)goods[i]=newTelevision();elseif(i%4==1)goods[i]=newComputer();elseif(i%4==2)goods[i]=newWashMachine();elseif(i%4==3)goods[i]=newRefrigerator();}Trucktruck=newTruck(goods);System.out.printf(\n货车装载的货物重量:%-8.5fkg\n,truck.getTotalWeights());}}截图实验3小狗的状态1.实验要求小狗崽不同环境下可能呈现不同的状态表现,要
本文标题:试验4
链接地址:https://www.777doc.com/doc-6048604 .html