您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 高中教育 > JAVA第六次实验报告-接口
实验一1、实验题目体操比赛计算选手成绩的办法是去掉一个最高分和一个最低分再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班学生的平均成绩。Gymnastics类和School类都实现了ComputerAverage接口,但实现方式不同。2、程序代码interfaceComputerAverage{publicdoubleaverage(doublex[]);}classGymnasticsimplementsComputerAverage{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[i];x[i]=x[j];x[j]=temp;}}}for(inti=1;icount-1;i++){aver=aver+x[i];}if(count2)aver=aver/(count-2);elseaver=0;returnaver;}}classSchoolimplementsComputerAverage{publicdoubleaverage(doublex[]){intcount=x.length;doubleaver=0;for(inti=0;icount;i++){aver=aver+x[i];}if(count0)aver=aver/count;returnaver;}}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};ComputerAveragecomputer;computer=newGymnastics();doubleresult=computer.average(a);//computer调用average(doublex[])方法,将数组a传递给参数xSystem.out.printf(%n);System.out.printf(体操选手最后得分:%5.3f\n,result);computer=newSchool();result=computer.average(b);//computer调用average(doublex[])方法,将数组b传递给参数xSystem.out.printf(班级考试平均分数:%-5.2f\n,result);}}3、实验结果4、实验分析一个类可以实现多个接口,类通过使用关键字implements声明自己实现一个或多个接口,如果一个非抽象类实现了某个接口,那么这个类必须重写该接口的所有方法。5、实验练习School类如果不重写publicdoubleaversge(doublex[])方法,程序编译时提示怎样的错误?答:SChool不是抽象的,并且未覆盖ComputerAverage中的抽象方法。实验二1、实验题目货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机,卡车需要计算出整批货物的重量。2、实验代码interfaceComputerWeight{publicdoublecomputerWeight();}classTelevisionimplementsComputerWeight{publicdoublecomputerWeight(){return3.5;}}classComputerimplementsComputerWeight{publicdoublecomputerWeight(){return2.67;}}classWashMachineimplementsComputerWeight{publicdoublecomputerWeight(){return13.8;}}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].computerWeight();}returntotalWeights;}}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());}}3、实验结果4、实验分析接口回调是指:可以把使用某一接口的类型创建的对象引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被实现的接口中的方法,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程成为对象功能的接口回调。接口的方法不一定相同,接口回调可能产生不同的行为。5、实验练习请在实验基础上再编写一个实现ComputerWeight接口的类,比如Refrigerrator。这样一来,货车装载的货物中就可以有Refrigerrator类型的对象。当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?答:代码:classRefrigerratorimplementsComputerWeight{publicdoublecomputerWeight(){return12.8;}}实验三1、实验题目小狗在不同环境条件下可能呈现不同的状态表现,要求接口封装小狗的状态。具体要求如下:(1)编写一个接口DogState,该接口有一个名为voidshowState()方法。(2)编写一个Dog类,该类中有一个DogState接口声明的变量state,另外,该类有一个show()方法,在该方法中让接口state回调showState()方法。(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。(4)编写主类,在主类中实现测试小狗的各种状态。2、程序代码interfaceDogState{publicvoidshowState();}classSoftlyStateimplementsDogState{publicvoidshowState(){System.out.println(听主人的命令);}}classMeetEnemyStateimplementsDogState{publicvoidshowState(){System.out.println(狂叫,并冲过去狠咬敌人);}}classMeetFriendStateimplementsDogState{publicvoidshowState(){System.out.println(晃动尾巴,表示欢迎);}}classMeetAnotherdogStateimplementsDogState{publicvoidshowState(){System.out.println(嬉戏);}}classDog{DogStatestate;publicvoidshow(){state.showState();}publicvoidsetState(DogStates){state=s;}}publicclassCheckDogState{publicstaticvoidmain(Stringargs[]){DogyellowDog=newDog();System.out.print(狗在主人面前:);yellowDog.setState(newSoftlyState());yellowDog.show();System.out.print(狗遇到敌人:);yellowDog.setState(newMeetEnemyState());yellowDog.show();System.out.print(狗遇到朋友:);yellowDog.setState(newMeetFriendState());yellowDog.show();System.out.print(狗遇到同类:);yellowDog.setState(newMeetAnotherdogState());yellowDog.show();}}3、实验结果4、实验分析面向接口编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向接口,即所设计中的重要数据是接口声明的变量,而不是具体声明的对象。5、实验练习用面向接口的思想编写一个程序,模拟水杯中的水在不同温度下可能出现的状态。代码:interfaceWaterState{publicvoidshowState();}classSubzeroStateimplementsWaterState{publicvoidshowState(){System.out.println(结冰);}}classNormalStateimplementsWaterState{publicvoidshowState(){System.out.println(冰冷或凉快);}}classHotStateimplementsWaterState{publicvoidshowState(){System.out.println(有热气冒出,温热);}}classBoiledStateimplementsWaterState{publicvoidshowState(){System.out.println(沸腾,烫);}}classWater{WaterStatestate;publicvoidshow(){state.showState();}publicvoidsetState(WaterStates){state=s;}}publicclassCheckWaterState{publicstaticvoidmain(Stringargs[]){WatercupWater=newWater();System.out.print(水杯中的水在零下时:);cupWater.setState(newSubzeroState());cupWater.show();System.out.print(水杯中的水在常温时:);cupWater.setState(newNormalState());cupWater.show(
本文标题:JAVA第六次实验报告-接口
链接地址:https://www.777doc.com/doc-6337273 .html