Java程序设计-试验3(学生版)_第1页
Java程序设计-试验3(学生版)_第2页
Java程序设计-试验3(学生版)_第3页
Java程序设计-试验3(学生版)_第4页
Java程序设计-试验3(学生版)_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、华北电力大学实 验 报 告 实验名称 Java程序面向对象设计(二) 课程名称 Java程序设计 专业班级: 学生姓名: 学 号: 成 绩:指导教师:张学斌 实验日期: 请浏览后下载,资料供参考,期待您的好评与关注!一、实验目的及要求1. 掌握类的继承。2. 掌握类的隐藏和覆盖。3. 掌握抽象类和抽象方法二、试验内容实验1 中国人、北京人和美国人1实验要求编写程序模拟中国人、美国人是人,北京人是人。出主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople。要求如下:l People类有权限是protected的double型成员

2、变量height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。l ChinaPeople类是People类的子类,新增了public void chinaGongfu()方法,要求Chinapeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。l AmericanPeople类是People的子类,新增public void

3、 americanBoxing()方法。要求Americanpeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。l BeijingPeople类是Chinapeople的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。 2程序模板People

4、.java public 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.java pu

5、blic class ChinaPeople extends People public 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(坐如钟,站如松,睡如弓); -AmericanPe

6、ople.java public class AmericanPeople extends People 【代码2】 /重写public void speakHello()方法,输出How do you do 【代码3】 /重写public void averageHeight()方法,输出Americans average height:176 cm public void averageWeight() weight = 75; System.out.println(Americans average weight:+weight+ kg); public void americanBox

7、ing() System.out.println(直拳、钩拳、组合拳); -BeijingPeople.java public class BeijingPeople extends ChinaPeople 【代码4】 /重写public void averageHeight()方法,输出:北京人的平均身高: 172.5厘米 【代码5】 /重写public void averageWeight()方法,输出:北京人的平均体重:70公斤 public void beijingOpera() System.out.println(花脸、青衣、花旦和老生); -Example.java public

8、 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.averageHe

9、ight(); americanPeople.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeight(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera() ; beijingPeople.chinaGongfu(); 实验2 银行利息计算1 实验要求

10、假设银行Bank()已经有了按整年year计算利息的一般方法,其中year只能取正整数。比如按整年计算的方法: double computerInterest() interest=year*0.35*savedMoney; return interest; 建设银行ConstructionBank是Bank的子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量,比如,当year取值5.216时,表示要计算5年零216天的利息,但希望首先按银行Bank的方法computerInterest()计算出5整年的利息,然后再计算216天的利息。那么,

11、建设银行就必须把5.216的整数部分赋给隐藏的year,并让super调用隐藏的,按整年计算利息的方法。要求ConstructionBank和BankOfDalian类是Bank类的子类,ConstructionBank和BankOfDalian都使用super调用隐藏的成员变量和方法。2程序模板 Bank.javapublic class Bank int savedMoney; int year; double interest; double interestRate = 0.29; public double computerInterest() interest=year*inter

12、estRate*savedMoney; return interest; public void setInterestRate(double rate) interestRate = rate; - ConstructionBank.javapublic class ConstructionBank extends Bank double year; public double computerInterest() super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000); double yearIntere

13、st = 【代码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 Bank double year; public do

14、uble 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.ye

15、ar,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(); Ba

16、nkOfDalian 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 实验扩展 参照建设银行或大连银行,再编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息 实验3 公司支出的总薪水 1 实验要求 有一个abstract类

17、,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按州领取薪水。Employee类有一个abstract方法: Public abstract earnings( ); 子类必须重写父类的earnings( )方法,给出各自领取报酬的具体公式。 有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的单元可以是YearWorker对象的向上转型对象、MonthWorker对象的向上转型对象或WeekW

18、orker对象的向上转型对象。程序能输出Company对象一年需要支付的薪水总额。 2 程序模板 ComapanySalary.javaabstract class Employee public abstract double earnings();class YearWorker extends Employee 【代码1】 /重写earnings()方法class MonthWorker extends Employee 【代码2】 /重写earnings()方法class WeekWorker extends Employee 【代码3】 /重写earnings()方法。class C

19、ompany Employee employee; double salaries=0; Company(Employee employee) this.employee=employee; public double salariesPay() salaries=0; 【代码4】 /计算salaries。 return salaries; public class CompanySalary public static void main(String args) Employee employee=new Employee29; /公司有29名雇员 for(int i=0;iemployee.length;i+) /雇员简单地分成三类 if(i%3=0) employeei=new W

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论