已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五 继承与接口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方法。public class qiuhe public static void main(String args) long sum=0; System.out.println(1!+3!+5!+7!+9!+10!=); for(int i =1; i=10 ; i+=2) sum = sum + method(i); /调用方法 System.out.println(sum); /使用递归方法 static long method(int index) if(index=1) return 1; else return index*method(index-1); public class qiuhe public static void main(String args) long sum=0; System.out.println(1!+3!+5!+7!+9!+10!=); for(int i =1; i=10 ; i+=2) sum = sum + method(i); /调用方法 System.out.println(sum); /使用递归方法 static long method(int index) if(index=1) return 1; else return index*method(index-1); 3. 实验步骤参考实验指导书中P26-37页的实验步骤。4. 评分标准1. A内容功能完善,编程风格好,人机接口界面好; 2. B内容功能完善,编程风格良好,人机接口界面良好;3. C完成必做内容;4. D能完成必做内容;5. E未按时完成必做内容,或者抄袭(雷同者全部为E).参照书上实验按模版要求,将【代码】替换为Java程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。(压缩包的文件名为:学号后三位和名字开头字母,如109zhhRAR|ZIP)实验1 中国人与美国人模板代码 People.javapublic class People protected double weight ,height;public void speakHello()System.out.println(yayayaya);public void averageHeight()height=173;System.out.println(average height:+height);public void averageWeight()weight=70;System.out.println(average weight:+weight);ChinaPeople.javapublic class ChinaPeople extends Peoplepublic void speakHello()System.out.println(你好);public void averageHeight()height=168.78;System.out.println(中国人的平均身高:+height+厘米);/【代码1】/重写public void averageWeight()方法,输出:中国人的平均体重:65公斤public void chinaGongfu()System.out.println(坐如钟,站如松,睡如弓);AmericanPeople.javapublic class AmericanPeople extends People/【代码2】/重写public void speakHello()方法,输出:How do you do/【代码3】/重写public void averageHeight()方法,输出:Americans average height:176cmpublic void averageWeight()weight=75;System.out.println(Americans average weight:+weight+kg);public void americanBoxing()System.out.println(直拳、钩拳、组合拳);BeijingPeople.javapublic class BeijingPeople extends ChinaPeople/【代码4】/重写public void averageHeight()方法,输出:北京人的平均身高:172.5厘米/【代码5】/重写public void averageWeight()方法,输出:北京人的平均体重:70公斤public void beijingOpera()System.out.println(花脸、青衣、花旦和老生);Example.javapublic class Example public static void main(String args) ChinaPeople chinaPeople = new ChinaPeople();AmericanPeople americanPeople = new AmericanPeople();BeijingPeople beijingPeople = new BeijingPeople();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.javapublic class Bank int savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest()interest=year*interestRate*savedMoney;return interest;public void setInterestRate(double rate) interestRate = rate;ConstructionBank.javapublic class ConstructionBank extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=/【代码1】/super调用隐藏的computerInterest()方法double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在建设银行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;BankOfDalian.javapublic class BankOfDalian extends Bankdouble year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=/【代码2】/super调用隐藏的computerInterest()方法double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(%d元存在大连银行%d年零%d天的利息:%f元n,savedMoney,super.year,day,interest);return interest;SaveMoney.javapublic class SaveMoney public static void main(String args) int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=puterInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=puterInterest();System.out.printf(两个银行利息相差%f元n,interest2-interest1);实验3 面积之和模板代码 Geometry.javapublic abstract class Geometry public abstract double getArea();TotalArea.javapublic class TotalArea Geometry tuxing;double totalArea=0;public void setTuxing(Geometry t)tuxing=t;public double computerTatalArea()/【代码3】/用循环语句让tuxing的元素调用getArea方法,并将返回的值累加到totalAreareturn totalArea;Rect.javapublic class Rect extends Geometrydouble a,b;Rect(double a,double b)this.a=a;this.b=b;/【代码1】/重写getArea()方法Circle.javapublic class Circle extends Geometrydouble r;Circle(double r)this.r=r;/【代码2】/重写getArea()方法MainClass.javapublic class MainClass public static void main(String args) Geometry tuxing=new Geometry29;/有29个Geometry对象for(int i=0;ituxing.length;i+)/29个Geometry对象分成两类if(i%2=0)tuxingi=new Rect(16+i,68);else if(i%2=1)tuxingi=new Circle(10+i);TotalArea computer=new TotalArea();computer.setTuxing(tuxing);System.out.printf(各种图形的面积之和:n%f,puterTatalArea();实验4 歌手大赛模板代码 ComputerAverage.javapublic interface ComputerAverage public double average(double x);SongGame.javapublic class SongGame implements ComputerAveragepublic double average(double x)int count=x.length;double aver=0,temp=0;for(int i=0;icount;i+)for(int j=0;jcount;j+)if(xjxi)temp=xj;xj=xi;xi=temp;for(int i=0;i2)aver=aver/(count-2);elseaver=0;return aver;School.javapublic class School implements ComputerAverage/【代码1】/重写public double average(double x)方法,返回数组x的元素的算术平方Estimator.javapublic class Estimator public static void main(String args) double a=;double b=;ComputerAverage computer;computer=new SongGame();double result=/【代码2】/computer调用average(x)方法,将数组a传递给参数xSystem.out.printf(%n);System.out.printf(歌手最后得分:%5.3fn,result);computer=new School();result=/【代码3】/computer调用average(x)方法,将数组b传递给参数xSystem.out.printf(学生平均体重:%-5.2f kg,result);实验5 天气预报模板代码 WeatherState.javapublic interface WeatherState public void showState();Weather.javapublic class Weather WeatherState state;public void show()state.showState();public void setState(WeatherState s)state=s;CloudyLittleState.javapublic class CloudyLittleState implements WeatherStatepublic void showState()System.out.print(少云,有时晴。);CloudyDayState.javapublic class CloudyDayState implements WeatherState/【代码1】/重写public void showState()方法HeavyRainState.javapublic class HeavyR
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年环境监测试题及答案
- 2025年环境废水运维考试试题及答案
- 2025年河南公路养护技师考试题及答案
- 2025-2030中国液体化工物流行业客户满意度调查与提升策略报告
- 2026年中国苗圃项目经营分析报告
- 去小兔家做客中班健康教案(2025-2026学年)
- 北师大版高一数学必修一集合教案(2025-2026学年)
- 微格教学教案模板高中生物(2025-2026学年)
- 2026年中国氧气面罩项目经营分析报告
- 2026年中国视频剪辑软件项目经营分析报告
- 网点负责人考试题库考点
- 2025年呼和浩特天骄航空有限公司招聘笔试冲刺题(带答案解析)
- 结直肠癌导致急性肠梗阻外科治疗中国专家共识(2025版)课件
- 辅助改方时方向继电器电路识读穆中华60课件
- 东方航空民航招飞面试常见问题及答案
- 危险性较大的分部分项工程清单
- 英语第二册(五年制高职) 课件 Unit5 Social Rules
- 银行物业年终工作总结
- 2025年三方询价单合同模板
- ISO14001-2015环境管理体系风险和机遇识别评价分析及应对措施表(包含气候变化)
- 如何正确书写化学方程式 教学设计
评论
0/150
提交评论