


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
..实用文档.实用文档.华北电力大学实验报告实验名称 Java程序面向对象设计〔二〕课程名称 Java程序设计专业班级学 号:
学生姓名成 绩:指导教师:张学斌 实验日期:一、实验目的及要求掌握类的继承。掌握类的隐藏和覆盖。掌握抽象类和抽象方法二、试验内容实验1中国人、北京人和美国人实验要求编写程序模拟中国人、美国人是人,北京人是人。出主类外,程序中还有4个类ChinaPeoplAmericanPeopl和BeijingPeope要求如下:People类有权限是protecte的double型成员变量height和weightcspeakHellopublicvoidaverageHeihblicvoidaverageWeihChinaPeopl类是PeoplpublicvoidchinaGonguChinapeople重写父类的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight()方法。AmericanPeople类是People的子类,新增publicvoidamericanBoxing(方)法。要求Americanpeopl重写父类的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight法。BeijingPeope是ChinapeoplpublicvoidbeijingOprChinaPeople重写父类的publicvoidspeakHelicvoidaverageHeihblicvoidaverageWeight()方法。程序模板publicclassPeople{protecteddoubleweight,height;publicvoidspeakHello(){System.out.println("yayayaya");}publicvoidaverageHeight(){height=173;System.out.println("averageheight:"+height);}publicvoidaverageWeight(){weight=70;System.out.println("averageweight:"+weight);}}publicclassChinaPeopleextendsPeoplepublicvoidspeakHello(){System.out.printn}publicvoidaverageHeight(){height=168.78;System.out.println(":"+height+"厘米");}【代码1】//重写publicvoidaverageWeight()方法,输出:"中国人的平均体重:65公斤"publicvoidchinaGongfu(){System.out.println("坐如钟,站如松,睡如弓");}}publicclassAmericanPeopleextendsPeople{【代码2】//重写publicvoidspeakHello()方法,输出"Howdoyoudo"【代码3】//重写publicvoidaverageHeight()方法,输出"American'saverageheight:176cm"publicvoidaverageWeight(){weight=75;System.out.println("American'saverageweight:"+weight+"kg");}publicvoidamericanBoxing(){System.out.println("直拳、钩拳、组合拳");}}publicclassBeijingPeopleextendsChinaPeople{【代码4】//重写publicvoidaverageHeight()方法,输出:"北京人的平均身高:172.5厘米"【代码5】//重写publicvoidaverageWeight()方法,输出:"北京人的平均体重:70公斤"publicvoidbeijingOpera(){System.out.println("花脸、青衣、花旦和老生");}}publicclassExample{publicstaticvoidmain(Stringargs[]){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()已经有了按整年year计算利息的一般方法,其中year只能取正整数。比方按整年计算的方法:doublecomputerInterest(){interest=year**savedMoney;returninterest;}ConstructionBank是Bank法,即自己声明一个doubleyear变量,比方,当year5.2165216天的利息,但希望首先按银行BankcomputerInterest()5216天5.216的整数局部赋给隐藏的year,并让super年计算利息的方法。要求ConstructionBank和BankOfDalian类是Bank类的子类,ConstructionBank和BankOfDalian都使用super调用隐藏的成员变量和方法。程序模板publicclassBank{intsavedMoney;intyear;doubleinterest;doubleinterestRate=0.29;publicdoublecomputerInterest(){interest=year*interestRate*savedMoney;returninterest;}publicvoidsetInterestRate(doublerate){interestRate=rate;}}publicclassConstructionBankextendsBank{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;}}publicclassBankOfDalianextendsBank{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;}}publicclassSaveMoney{publicstaticvoidmain(Stringargs[]){intamount=8000;ConstructionBankbank1=newConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);doubleinterest1=bank1 puterInterest();BankOfDalianbank2=newBankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);doubleinterest2=bank2 puterInterest();System.out.printf("两个银行利息相差%f元\n",interest2-interest1);}}实验扩展80008236天的利息实验3 公司支出的总薪水实验要求有一个abstractEmployeeEmployee的子类有MonthWorker和YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorkerEmployee类有一个abstract方法:Publicabstractearnings();子类必须重写父类的earnings()方法给出各自领取报酬的具体公式。有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的单元可以是YearWorker对象的向上转型对象、MonthWorker对象的向上转型对象或WeekWorker对象的向上转型对象。程序能输出Company对象一年需要支付的薪水总额。程序模板abstractclassEmployee{publicabstractdoubleearnings();}classYearWorkerextendsEmployee{1】//重写earnings()方法}classMonthWorkerextendsEmployee{【代码2】//重写earnings()方法}classWeekWorkerextendsEmployee{【代码3】//重写earnings()方法。}classCompany{Employee[]employee;doublesalaries=0;Company(Employee[]employee){this.employee=employee;}publicdoublesalariesPay(){salaries=0;【代码4】//计算salaries。returnsalaries;}}publicclassCompanySalary{publicstaticvoidmain(Stringargs[]){Employee[]employee=newEmployee[29];公司有29名雇for(inti=0;i<employee.length;i++){ //雇员简单地分成三类if(i%3==0)employee[i]=newelseif(i%3==1)employee[i]=newMonthWorker();elseif(i%3==2)employee[i]=newYearWorker();}Companycompany=newCompany(employee);元}}实验指导对于【代码2】,一年按12个月计算出雇员的一年的年薪,比方:publicdoubleearnings(){return12*2300;}尽管
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 第15课《我们不乱扔》教学设计-2024-2025学年一年级道德与法治上册统编版
- 展览馆装修合同
- 2025年度建筑企业农民工劳动合同创新模式试点方案
- 2025年度五星级酒店与VIP客人个性化服务协议
- 2025年度房产赠与与可持续发展合同
- 2025年度冷链物流货运损坏赔偿协议书
- 二零二五年度人工智能教育平台合作协议中的支付及费用分摊细则
- 2025年度带宠物友好房屋出租协议电子版
- 2025年度广告代理合同解除通知期限与费用结算规范
- 2025年度报废车买卖及报废车辆拆解与环保设施投资合同
- 公司安全生产监督管理办法
- 钢筋工工艺与实习(第二版)课件汇总全书电子教案完整版课件最全幻灯片(最新)课件电子教案幻灯片
- 煤矿从业人员考试题库全答案(word版)
- 洞顶回填技术交底
- 最简易的帕累托图制作方法简介PPT通用课件
- 城市轨道交通应急处理课程标准
- 初二下分式混合计算练习1(附答案)
- (完整版)振幅调制与解调习题及其解答
- 抗震支架施工安装合同
- JJG 657-2019 呼出气体酒精含量检测仪 检定规程(高清版)
- 政法书记在全县公安工作会议上的讲话
评论
0/150
提交评论