JAVA第六次实验分析报告接口.docx

上传人:b****5 文档编号:6927190 上传时间:2023-01-12 格式:DOCX 页数:10 大小:40.06KB
下载 相关 举报
JAVA第六次实验分析报告接口.docx_第1页
第1页 / 共10页
JAVA第六次实验分析报告接口.docx_第2页
第2页 / 共10页
JAVA第六次实验分析报告接口.docx_第3页
第3页 / 共10页
JAVA第六次实验分析报告接口.docx_第4页
第4页 / 共10页
JAVA第六次实验分析报告接口.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

JAVA第六次实验分析报告接口.docx

《JAVA第六次实验分析报告接口.docx》由会员分享,可在线阅读,更多相关《JAVA第六次实验分析报告接口.docx(10页珍藏版)》请在冰豆网上搜索。

JAVA第六次实验分析报告接口.docx

JAVA第六次实验分析报告接口

JAVA第六次实验报告-接口

 

 

————————————————————————————————作者:

————————————————————————————————日期:

 

实验一

1、实验题目

体操比赛计算选手成绩的办法是去掉一个最高分和一个最低分再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班学生的平均成绩。

Gymnastics类和School类都实现了ComputerAverage接口,但实现方式不同。

2、程序代码

interfaceComputerAverage{

publicdoubleaverage(doublex[]);

}

classGymnasticsimplementsComputerAverage{

publicdoubleaverage(doublex[]){

intcount=x.length;

doubleaver=0,temp=0;

for(inti=0;i

for(intj=i;j

if(x[j]

temp=x[i];

x[i]=x[j];

x[j]=temp;

}

}

}

for(inti=1;i

aver=aver+x[i];

}

if(count>2)

aver=aver/(count-2);

else

aver=0;

returnaver;

}

}

classSchoolimplementsComputerAverage{

publicdoubleaverage(doublex[]){

intcount=x.length;

doubleaver=0;

for(inti=0;i

aver=aver+x[i];

}

if(count>0)

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传递给参数x

System.out.printf("%n");

System.out.printf("体操选手最后得分:

%5.3f\n",result);

computer=newSchool();

result=computer.average(b);//computer调用average(doublex[])方法,将数组b传递给参数x

System.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;i

totalWeights=totalWeights+goods[i].computerWeight();

}

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());

}

}

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();

System.out.print("水杯中的水在六十七度时:

");

cupWater.setState(newHotState());

cupWater.show();

System.out.print("水杯中的水在一XX时:

");

cupWater.setState(newBoiledState());

cupWater.show();

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1