第八次实验报告
更新时间:2024-01-03 21:42:01 阅读量: 教育文库 文档下载
第八次实验
实验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的子类,新增了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 beijingOpera()方
法。
2.实验代码: //People.java
public class People { protected double weight,height; public void speakHello() { System.out.println(\ } public void averageHeight() { height=173; System.out.println(\ } public void averageWeight() { weight=70; System.out.println(\ } }
//ChinaPeople.java
public class ChinaPeople extends People { public void speakHello() { System.out.println(\您好\ } public void averageHeight() { height=168.78; System.out.println(\中国人的平均身高:\厘米\ } public void averageWeight() {
weight=65; System.out.println(\中国人的平均体重:\千克\ } public void chinaGongfu() { System.out.println(\坐如钟,站如松,睡如弓\ } }
//AmericanPeople.java
public class AmericanPeople extends People { public void speakHello () { System.out.println(\ } public void averageHeight() { height=176; System.out.println(\厘米\ } public void averageWeight() { weight=75; System.out.println(\ } public void americanBoxing() { System.out.println(\直拳,勾拳,组合拳\ } }
//BeijingPeople.java
public class BeijingPeople extends ChinaPeople { public void averageHeight() { height=172.5; System.out.println(\北京人的平均身高:\厘米\ } public void averageWeight() { weight=70; System.out.println(\北京人得平均体重:\千克\ } public void beijingOpera() { System.out.println(\花脸、青衣、花旦和老生\ } }
//Example.java
public class Example { public static void main(String arg[]) {
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(); } }
3.实验结果:
4.实验分析:
(1) 方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。
这样,子类继承的方法才能被隐藏。
(2) 子类在重写方法时,如果重写的方法是static方法,static关键字必须保留;如果重写的
方法是实例方法,重写时不可以用static修饰。
(3) 如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法
可以改变父类的具遗体行为。
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; }
建设银行constructionBank是bankde 子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量。要求constructionbank和bankofDalian类是bank类的子类,constructionbank和bankofdalian都使用super调用隐藏的按整年计算利息的方法。
2.实验代码:
//Bank.java
public 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.java
public class ConstructionBank extends Bank{ double year; public double computerInterest(){ super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000); double yearInterest=super.computerInterest(); double dayInterest=day*0.0001*savedMoney; interest=yearInterest+dayInterest; System.out.printf(\元存在建设银行%d年零%d天的利息:%f元\\n\ return interest; } }
// BankOfDalian.java
public class BankOfDalian extends Bank { double year;
public double computerInterest(){ super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000);
double yearInterest=super.computerInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest;
System.out.printf(\元存在大连银行%d年零%d天的利息:%f元\\n\ return interest; } }
// SaveMoney.java
public 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=bank1.computerInterest(); BankOfDalian bank2=new BankOfDalian(); bank2.savedMoney=amount; bank2.year=8.236; bank2.setInterestRate(0.035); double interest2=bank2.computerInterest(); System.out.printf(\两个银行利息相差%f元\\n\ } }
3.实验结果:
4.实验分析:
(1) 子类不继承父类的构造方法,因此子类在其构造方法中需使用super来调用父类的
构造方法,并且super必须是子类构造方法中的头一条语句。
(2) 当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量
或继承的成员变量。
5.实验后的练习:
参照建设银行或大连银行,在编写一个商业银行,让程序输出8000元存在商业银行8年零236天的利息。 //Bank.java
public 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; } }
// CommercialBank
public class CommercialBank extends Bank { double year;
public double computerInterest(){ super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000);
double yearInterest=super.computerInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest;
System.out.printf(\元存在商业银行%d\\n\ return interest; } }
// SaveMoney.java
public class SaveMoney{ public 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=bank.computerInterest(); } }
年零%d天的利息:%f元
实验3:公司支出的总薪水 1.实验要求:
要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker、WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水、WeekWorker按周领取的薪水。Employee类有一个abstract方法: public abstract earnings();
子类必须重写父类的earings()方法,给出各自领取报酬的具体方式。
有一个Company类,该类用Employee对象数组作为成员,Employee对象数组的单元可以是YearWorker对象的上转型对象、MonthWorker独享的上转型独享或weekworker对象的上转型独享。程序能输出Company对象一年需要支付的薪水总额。
2.实验代码:
abstract class Employee{ public abstract double earnings(); }
class YearWorker extends Employee{ public double earnings(){ return 12000; } }
class MonthWorker extends Employee{ public double earnings(){ return 12*2300; } }
class WeekWorker extends Employee{ public double earnings(){ return 52*780; } }
class Company{ Employee[] employee; double salaries=0; Company(Employee[] employee){ this.employee=employee; } public double salariesPay(){ salaries=0; for(int i=0;i public class CompanySalary{ public static void main(String args[]){ Employee[] employee=new Employee[29]; for(int i=0;i 3.实验结果: 4.实验分析: 尽管abstract类不能创建对象,但abstract类声明的对象可以存放子类对象的引用,即成为子类对象的上转型对象。 5.实验后的练习: (1) 子类yearworker如果不重写earnings()方法,程序编译是提示怎么样的错误。 YearWorker 不是抽象的,并且未覆盖 Employee 中的抽象方法 earnings() class YearWorker extends Employee{ (2) 在增加一种雇员,并计算公司一年的总薪水。 class DayWorker extends Employee{ public double earnings(){ return 365*100; } } 将for语句改写为: for(int i=0;i
正在阅读:
第八次实验报告01-03
装配钳工鉴定初级试题及答案(1)06-23
Java实验教案09-22
AC-20C沥青混合料配合比设计06-16
交变电流的产生和描述06-23
第3章使用DTD和Schema08-06
负债教案01-22
医院消毒卫生标准08-06
全县办公室系统业务知识竞赛复习题库09-30
济宁学院计量经济学期末练习题07-08
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 实验
- 报告
- 2014电大计算机应用基础(Win7)形成性考核册(含操作题)
- 2019-2020学年八年级物理上册 第六章 质量和密度单元综合检测(新版)新人教版
- 2015--2016学年度盐城市冈中小学数学二年级上册期中试卷
- 2017年高考语文作文社会热点素材及运用
- 物理变化和化学变化教学设计24
- 2018教科版最新六年级上册综合实践教案
- 2014年12月N1能力考试题解析部分 - 图文
- 2013年塑裤市场分析报告
- 人教版一年级上册道德与法治教案全册
- 徐州市2012-2013学年度高三第一次质量检测
- 组织行为学习题1
- 2017年清远市公卫执业医师备考练习题八
- 新能源汽车及零部件检验检测公共服务平台建设可行性研究报告 - 图文
- 离心泵的课程设计-第七组-胡极诸 - 图文
- 最新雕刻时光咖啡实习周记原创
- 北师大版八年级下册《三角形的证明》经典例题
- 兰州大学《保险学原理》15秋在线作业2满分答案
- 基于单片机的红外感应报警系统设计论文(传感器 - 程序 - 原理图全套)分解
- 商场专柜合作协议
- 顶驱常见故障分析