试验4.docx
- 文档编号:26757206
- 上传时间:2023-06-22
- 格式:DOCX
- 页数:17
- 大小:36.08KB
试验4.docx
《试验4.docx》由会员分享,可在线阅读,更多相关《试验4.docx(17页珍藏版)》请在冰豆网上搜索。
试验4
试验4
华北电力大学
实验报告
|
|
实验名称接口与实现接口的类
课程名称Java程序设计
|
|
专业班级:
信管1301学生姓名:
王雯敏
学号:
************成绩:
指导教师:
张学斌实验日期:
2015.4.20
一、实验目的和要求
1理解接口的作用,理解接口和实现接口的类的关系
2掌握声明接口,一个类实现多个接口的声明和使用方法
3理解内嵌类型的概念,掌握声明内部类的方法
二、实验环境
Windows2000/WindowsXP,JDK1.2~1.6
三、实验内容和步骤
实验1评价成绩
1.实验要求
体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。
Gymmastics类和School类都实现了ComputerAverage接口,但实现的方式不同。
2.程序模板
Estimatior.java
interfaceCompurerAverage{
publicdoubleaverage(doublex[]);
}
classGymnasticsimplementsCompurerAverage{
publicdoubleaverage(doublex[]){
intcount=x.length;
doubleaver=0,temp=0;
for(inti=0;i for(intj=i;j if(x[j] temp=x[j]; x[j]=x[i]; x[i]=temp; } } } for(inti=1;i aver=aver+x[i]; } if(count>2) aver=aver/(count-2); else aver=0; 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传递给参数x System.out.printf("%n"); System.out.printf("体操选手最后得分: %5.3f\n",result); computer=newSchool(); result=【代码3】//computer调用average(doublex[])方法,将数组b传递给参数x System.out.printf("班级考试平均分数: %-5.2f",result); } } 实验结果 【代码1】 publicdoubleaverage(doublex[]){ doubleaver=0; for(inti=0;i aver+=x[i]; aver/=x.length; returnaver; } 【代码2】computer.average(a); 【代码3】computer.average(b); 3.实验指导 ●可以把实现某一接口的类创建的对象的引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被类实现的接口方法。 ●接口产生的多态就是指不同类在实现同一个接口时可能具有不同的实现方式。 实验2货车的装载量 1.实验要求 货车要装载一批货物,货物由三种商品组成: 电视、计算机和洗衣机。 卡车需要计算出整批货物的重量。 要求有一个ComputerWeight接口,该接口中有一个方法: publicdoublecomputerWeight() 有三个实现该接口的类: Television、Computer和WashMachine。 这三个类通过实现接口computerTotalSales给出自重。 有一个Truck类,该类用ComputerWeight接口类型的数组作为成员(Truck类面向接口),那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。 程序能输出Truck对象所装载的货物的总重量。 2.程序模板 CheckCarWeight.java interfaceComputerWeight{ 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; } publicdoublegetTotalWeights(){ totalWeights=0; 【代码4】//计算totalWeights returntotalWeights; } } publicclassCheckCarWeight{ publicstaticvoidmain(Stringargs[]){ ComputerWeight[]goods=newComputerWeight[650];//650件货物 for(inti=0;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;i if(i%2==0) goods[i]=newTelevision(); else goods[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】 for(inti=0;i totalWeights=totalWeights+goods[i].computeWeight(); 3.实验指导 ●对于【代码1】,可以简单返回一个值表示货物的重量,比如: publicdoublecomputerWeight(){ returen3.5 } ●对于数组goods的每个单元存放的是实现ComputerWeight接口的对象的引用,实验中的【代码4】通过循环语句让数组的每个单元调用computerWeight()方法,并将该方法返回的值累加到totalWeight,如下所示: for(inti=0;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(){ 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;i totalWeights=totalWeights+goods[i].computeWeight(); //计算totalWeights returntotalWeights; } } publicclassCheckCarWeight{ publicstaticvoidmain(Stringargs[]){ ComputerWeight[]goods=newComputerWeight[650];//650件货物 for(inti=0;i 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.实验要求 小狗崽不同环境下可能呈现不同的状态表现,要求用接口封装小狗的状态。 具体要求如下: (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("狂叫,并冲向去很咬敌人"); }//【代码1】//publicvoidshowState() } classMeetFriendStateimplementsDogState{ publicvoidshowState(){ System.out.println("晃动尾巴,表示欢迎"); }//【代码2】//重写publicvoidshowState()方法 } classMeetAnotherDogimplementsDogState{ publicvoidshowState(){ System.out.println("嬉戏"); }//【代码3】//重写publicvoidshowState()方法 } 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(newMeetAnotherDog()); yellowDog.show(); } } 实验结果 【代码1】 publicvoidshowState(){ System.out.println("狂叫,并冲向去很咬敌人"); } 【代码2】 publicvoidshowState(){ System.out.println("晃动尾巴,表示欢迎"); } 【代码3】 publicvoidshowState(){ System.out.println("嬉戏"); } 3.实验扩展 用面向接口的思想编写程序,模拟水杯中的水在不同温度下可能出现的状态。 代码 interfaceWaterState{ publicvoidshowState(); } classUnderZeroimplementsWaterState{ publicvoidshowState(){ System.out.println("固态"); } } classMiddleimplementsWaterState{ publicvoidshowState(){ System.out.println("液态"); } } classBeyoundOneHundredimplementsWaterState{ publicvoidshowState(){ System.out.println("气态"); } } classWater{ WaterStatestate; publicvoidshow(){ state.showState(); } publicvoidsetState(WaterStates){ state=s; } } publicclassCheckWaterState{ publicstaticvoidmain(Stringargs[]){ Waterawater=newWater(); System.out.print("0度以下: "); awater.setState(newUnderZero()); awater.show(); System.out.print("0到100度: "); awater.setState(newMiddle()); awater.show(); System.out.print("100度以上: "); awater.setState(newBeyoundOneHundred()); awater.show(); } } 截图 实验4二维图形 1.实验要求 声明椭圆类Ellipse,实现Area接口,计算椭圆面积 2.程序结果 实验结果 代码 interfaceArea{ publicdoublecomputeArea(); } publicclassEllipseimplementsArea{ doubler1,r2; Ellipse(doubler1,doubler2){ this.r1=r1; this.r2=r2; } publicdoublecomputeArea(){ returnr1*r2*Math.PI; } publicstaticvoidmain(String[]args){ Ellipsee=newEllipse(2,4); System.out.print("椭圆面积为: "+puteArea()); } } 截图 实验5三维物体 1.实验要求 声明圆柱体类和圆锥类,继承矩形类Rectangle并实现Volume接口,计算表面积和体积 2.程序结果。 实验结果 代码 interfaceVolume{ publicdoublegetArea(); publicdoublegetVolume(); } classRectangle{ doubleh,r; } classColumnextendsRectangleimplementsVolume{ publicColumn(doubleh,doubler){ this.h=h; this.r=r; } publicdoublegetArea(){ return2*Math.PI*r*r+2*Math.PI*r*h; } publicdoublegetVolume(){ returnMath.PI*r*r*h; } } classConeextendsRectangleimplementsVolume{ doubleh,r,l; publicCone(doubleh,doubler,doublel){ this.h=h; this.r=r; this.l=l; } publicdoublegetArea(){ returnMath.PI*r*l+Math.PI*r*r; } publicdoublegetVolume(){ return(Math.PI*r*r*h)/3; } } publicclassCompute{ publicstaticvoidmain(String[]args){ Columncol=newColumn(8.0,4.0); System.out.println("圆柱的表面积: "+col.getArea()); System.out.println("圆柱的体积: "+col.getVolume()); Conecon=newCone(8.0,4.0,5.0); System.out.println("圆锥的表面积: "+con.getArea()); System.out.println("圆锥的体积: "+con.getVolume()); } } 截图
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 试验