ImageVerifierCode 换一换
格式:DOC , 页数:11 ,大小:147.54KB ,
资源ID:3676130      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-3676130.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(第八次实验报告.doc)为本站会员(weiwoduzun)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

第八次实验报告.doc

1、第八次实验实验 1:中国人、北京人和美国人1.实验要求:编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有 4 个类:People、ChinaPeople、AmericanPeople 和 BeijingPeople 类。要求如下:(1) People 类有权限是 protected 的 double 型成员变量 height 和 weight,以及public void speakHello() 、public void averageHeight()和 public void averageWeight()方法。(2) ChinaPeople 类是 People 的子类,

2、新增了 public void averageHeight()和public voidaverageWeight()方法。(3) AmericanPeople 类是 People 的子类,新增方法 public void AmericanBoxing() 。要求 AmericanPeople 重写父类的 public void speakHello() 、public void averageHeight()和 public void averageWeight()方法。(4) BeijingPeople 类是 ChinaPeople 的子类,新增 public void beijingOpe

3、ra()方法。2.实验代码:/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);

4、/ChinaPeople.javapublic class ChinaPeople extends People public void speakHello() System.out.println(“您好“);public void averageHeight() height=168.78;System.out.println(“中国人的平均身高:“+height+“厘米“);public void averageWeight() weight=65;System.out.println(“中国人的平均体重:“+weight+“千克“);public void chinaGongfu()

5、 System.out.println(“坐如钟,站如松,睡如弓 “);/AmericanPeople.javapublic class AmericanPeople extends People public void speakHello () System.out.println(“How do you do“);public void averageHeight() height=176;System.out.println(“Americans average height:“+height+“厘米“);public void averageWeight() weight=75;Sy

6、stem.out.println(“Americans average weight:“+weight+“ kg“);public void americanBoxing() System.out.println(“直拳,勾拳,组合拳“);/BeijingPeople.javapublic class BeijingPeople extends ChinaPeople public void averageHeight() height=172.5;System.out.println(“北京人的平均身高:“+height+“厘米“);public void averageWeight() w

7、eight=70;System.out.println(“北京人得平均体重:“+weight+“千克“);public void beijingOpera() System.out.println(“花脸、青衣、花旦和老生“);/Example.javapublic class Example public static void main(String arg) ChinaPeople chinaPeople=new ChinaPeople();AmericanPeople americanPeople=new AmericanPeople();BeijingPeople beijingPe

8、ople=new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.china

9、Gongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();3.实验结果:4.实验分析:(1) 方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。这样,子类继承的方法才能被隐藏。(2) 子类在重写方法时,如果重写的方法是 static 方法,static 关键字必须保留;如果重写的方法是实例方法,重写时不可以用 static 修饰。(3) 如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法可以改变父类的具遗体

10、行为。5.实验后的练习:People 类中的public void speakHello() public void averageHeight() public void averageWeight() 三个方法的方法体中的语句是否可以省略。答:可以省略,因为省略后结果没有变化 实验 2:银行计算利息1.实验要求:假设银行 bank 已经有了按整年 year 计算利息的一般方法,其中 year 只能取正整数。比如,按整年计算的方法:Double computerInternet()Interest=year*0.35*saveMoney;Return interest;建设银行 constr

11、uctionBank 是 bankde 子类,准备隐藏继承的成员变量 year,并重写计算利息的方法,即自己声明一个 double 型的 year 变量。要求 constructionbank 和 bankofDalian类是 bank 类的子类,constructionbank 和 bankofdalian 都使用 super 调用隐藏的按整年计算利息的方法。2.实验代码:/Bank.javapublic class Bankint savedMoney;int year;double interest;double interestRate=0.29;public double compu

12、terInterest()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

13、);double yearInterest=puterInterest();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 Bank double year;public double co

14、mputerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=puterInterest();double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(“%d 元存在大连银行%d 年零%d 天的利息:%f 元n“,savedMoney,super.year,day,interest);return interest;/ Sa

15、veMoney.javapublic class SaveMoneypublic 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;ban

16、k2.year=8.236;bank2.setInterestRate(0.035);double interest2=puterInterest();System.out.printf(“两个银行利息相差%f 元n“,interest2-interest1);3.实验结果:4.实验分析:(1) 子类不继承父类的构造方法,因此子类在其构造方法中需使用 super 来调用父类的构造方法,并且 super 必须是子类构造方法中的头一条语句。(2) 当 super 调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量或继承的成员变量。5.实验后的练习:参照建设银行或大连银行,在编写一个商

17、业银行,让程序输出 8000 元存在商业银行 8 年零236 天的利息。/Bank.javapublic class Bankint 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;/ CommercialBankpublic clas

18、s CommercialBank extends Bank double year;public double computerInterest()super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=puterInterest();double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf(“%d 元存在商业银行%d 年零%d 天的利息:%f 元n

19、“,savedMoney,super.year,day,interest);return interest;/ SaveMoney.javapublic class SaveMoneypublic static void main(String args)int amount=8000;CommercialBank bank=new CommercialBank();bank.savedMoney=amount;bank.year=8.236;bank.setInterestRate(0.035);double interest=puterInterest();实验 3:公司支出的总薪水1.实

20、验要求:要求有一个 abstract 类,类名为 Employee。Employee 的子类有YearWorker、MonthWorker 、WeekWorker。YearWorker 对象按年领取薪水, MonthWorker按月领取薪水、WeekWorker 按周领取的薪水。Employee 类有一个 abstract 方法:public abstract earnings();子类必须重写父类的 earings()方法,给出各自领取报酬的具体方式。有一个 Company 类,该类用 Employee 对象数组作为成员,Employee 对象数组的单元可以是 YearWorker 对象的上

21、转型对象、 MonthWorker 独享的上转型独享或 weekworker 对象的上转型独享。程序能输出 Company 对象一年需要支付的薪水总额。2.实验代码:abstract class Employeepublic abstract double earnings();class YearWorker extends Employeepublic double earnings()return 12000;class MonthWorker extends Employeepublic double earnings()return 12*2300;class WeekWorker

22、extends Employeepublic double earnings()return 52*780;class CompanyEmployee employee;double salaries=0;Company(Employee employee)this.employee=employee;public double salariesPay()salaries=0;for(int i=0;iemployee.length;i+)salaries=salaries+employeei.earnings();return salaries;public class CompanySal

23、arypublic static void main(String args)Employee employee=new Employee29;for(int i=0;iemployee.length;i+)if(i%3=0)employeei=new WeekWorker();else if(i%3=1)employeei=new MonthWorker();else if(i%3=2)employeei=new YearWorker();Company company=new Company(employee);System.out.println(“公司薪水总额:“+company.sa

24、lariesPay()+“元“);3.实验结果:4.实验分析:尽管 abstract 类不能创建对象,但 abstract 类声明的对象可以存放子类对象的引用,即成为子类对象的上转型对象。5.实验后的练习:(1) 子类 yearworker 如果不重写 earnings()方法,程序编译是提示怎么样的错误。YearWorker 不是抽象的,并且未覆盖 Employee 中的抽象方法 earnings()class YearWorker extends Employee(2) 在增加一种雇员,并计算公司一年的总薪水。class DayWorker extends Employeepublic double earnings()return 365*100;将 for 语句改写为:for(int i=0;iemployee.length;i+)if(i%4=0)employeei=new WeekWorker();else if(i%4=1)employeei=new MonthWorker();else if(i%4=2)employeei=new YearWorker();else if(i%4=3)employeei=new DayWorker();

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报