您好,欢迎访问三七文档
实验五继承与接口1.实验目的1、掌握类的继承细节内容,包括子类的继承、子类对象创建、成员变量的继承与隐藏、方法的继承与重写2、掌握重写的目的以及怎样使用super关键字3、掌握上转型对象与接口回调技术4、掌握类怎样实现接口,及面向接口的编程思想2.实验内容1、参考实验指导书中P26-37页,完成下面实验:1)实验1中国人与美国人2)实验2银行与利息3)实验3面积之和4)实验4歌手大赛5)实验5天气预报2、完成习题:1)根据要求修改实验2,参照建设银行再编写一个商业银行(广发行),让程序输出8000元在广发行8年零212天的利息。2)根据要求修改实验3,再增加一种几何图形(梯形),并让主类中的tuxing的某些元素是梯形的上转型对象。3)仿照实验5编写一个程序实现模拟水杯中的水在不同温度下可能出现的状态。4)编写一个接口并创建两个实现该接口的类A、B。A、B类实现了接口的f方法,A类的f方法内容为计算1!+3!+5!……+9!并返回结果,B类的f方法内容为计算2!+4!+6!……+10!也返回结果;再编一个执行类,执行类运行时要求通过接口回调方式用同一个对象实例分别调用A类的f方法和B类的f方法。publicclassqiuhe{publicstaticvoidmain(Stringargs[]){longsum=0;System.out.println(1!+3!+5!+7!+9!……+10!=);for(inti=1;i=10;i+=2){sum=sum+method(i);//调用方法System.out.println(sum);}//使用递归方法staticlongmethod(intindex){if(index==1)return1;elsereturnindex*method(index-1);}}publicclassqiuhe{publicstaticvoidmain(Stringargs[]){longsum=0;System.out.println(1!+3!+5!+7!+9!……+10!=);for(inti=1;i=10;i+=2){sum=sum+method(i);//调用方法System.out.println(sum);}//使用递归方法staticlongmethod(intindex){if(index==1)return1;elsereturnindex*method(index-1);}}3.实验步骤参考实验指导书中P26-37页的实验步骤。4.评分标准1.A——内容功能完善,编程风格好,人机接口界面好;2.B——内容功能完善,编程风格良好,人机接口界面良好;3.C——完成必做内容;4.D——能完成必做内容;5.E——未按时完成必做内容,或者抄袭(雷同者全部为E).参照书上实验按模版要求,将【代码】替换为Java程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。(压缩包的文件名为:学号后三位和名字开头字母,如109zhh.RAR|ZIP)实验1中国人与美国人模板代码People.javapublicclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println(yayayaya);}publicvoidaverageHeight(){height=173;System.out.println(averageheight:+height);}publicvoidaverageWeight(){weight=70;System.out.println(averageweight:+weight);}}ChinaPeople.javapublicclassChinaPeopleextendsPeople{publicvoidspeakHello(){System.out.println(你好);}publicvoidaverageHeight(){height=168.78;System.out.println(中国人的平均身高:+height+厘米);}//【代码1】//重写publicvoidaverageWeight()方法,输出:中国人的平均体重:65公斤publicvoidchinaGongfu(){System.out.println(坐如钟,站如松,睡如弓);}}AmericanPeople.javapublicclassAmericanPeopleextendsPeople{//【代码2】//重写publicvoidspeakHello()方法,输出:Howdoyoudo//【代码3】//重写publicvoidaverageHeight()方法,输出:American'saverageheight:176cmpublicvoidaverageWeight(){weight=75;System.out.println(American'saverageweight:+weight+kg);}publicvoidamericanBoxing(){System.out.println(直拳、钩拳、组合拳);}}BeijingPeople.javapublicclassBeijingPeopleextendsChinaPeople{//【代码4】//重写publicvoidaverageHeight()方法,输出:北京人的平均身高:172.5厘米//【代码5】//重写publicvoidaverageWeight()方法,输出:北京人的平均体重:70公斤publicvoidbeijingOpera(){System.out.println(花脸、青衣、花旦和老生);}}Example.javapublicclassExample{publicstaticvoidmain(String[]args){ChinaPeoplechinaPeople=newChinaPeople();AmericanPeopleamericanPeople=newAmericanPeople();BeijingPeoplebeijingPeople=newBeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();}}实验2银行与利息模板代码Bank.javapublicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}ConstructionBank.javapublicclassConstructionBankextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=//【代码1】//super调用隐藏的computerInterest()方法doubledayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在建设银行%d年零%d天的利息:%f元\n,savedMoney,super.year,day,interest);returninterest;}}BankOfDalian.javapublicclassBankOfDalianextendsBank{doubleyear;publicdoublecomputerInterest(){super.year=(int)year;doubler=year-(int)year;intday=(int)(r*1000);doubleyearInterest=//【代码2】//super调用隐藏的computerInterest()方法doubledayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在大连银行%d年零%d天的利息:%f元\n,savedMoney,super.year,day,interest);returninterest;}}SaveMoney.javapublicclassSaveMoney{publicstaticvoidmain(String[]args){intamount=8000;ConstructionBankbank1=newConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);doubleinterest1=bank1.computerInterest();BankOfDalianbank2=newBankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);doubleinterest2=bank2.computerInterest();System.out.printf(两个银行利息相差%f元\n,interest2-interest1);}}实验3面积之和模板代码Geometry.javapublicabstractclassGeometry{publicabstractdoublegetArea();}TotalArea.javapublicclassTotalArea{Geometry[]tuxing;doubletotalArea=0;publicvoidsetTuxing(Geometry[]t){tuxing=t;}publicdoublecomputerTatalArea(){//【代码3】//用循环语句让tuxing的元素调用getArea方法,并将返回的值累加到totalAreareturntotalArea;}}Rect.javapublicclassRectextendsGeometry{doublea,b;Rect(doublea,doubleb){this.a=a;this.b=b;}//【代码1】//重写getArea()方法}Circle.javapublicclassCircleextendsGeometry{doubler;Circle(doubler){this.r=r;}//【代码2】//重写getArea()方法}MainClass.javapublic
本文标题:实验五-继承与接口
链接地址:https://www.777doc.com/doc-5741717 .html