资源描述
华北电力大学
实 验 报 告
实验名称 Java程序面向对象设计(二)
课程名称 Java程序设计
专业班级: 学生姓名:
学 号: 成 绩:
指导教师:张学斌 实验日期:
一、实验目得及要求
1、 掌握类得继承.
2、 掌握类得隐藏与覆盖。
3、 掌握抽象类与抽象方法
二、试验内容
实验1 中国人、北京人与美国人
1.实验要求
编写程序模拟中国人、美国人就是人,北京人就是人。出主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople与BeijingPeople。要求如下:
l People类有权限就是protected得double型成员变量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 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、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
public 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(”坐如钟,站如松,睡如弓");
}
}
---——-—----———--—--—-----———-—-—--——-—-—-——----——--——--————-—--—---———-—-———--—-—————--—--—--—-—------
AmericanPeople、java
public class AmericanPeople extends People {
【代码2】 //重写public void speakHello()方法,输出"How do you do"
【代码3】 //重写public void averageHeight()方法,输出”American's average height:176 cm"
public void averageWeight() {
weight = 75;
System、out、println(”American's average weight:"+weight+" kg");
}
public void americanBoxing() {
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 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 银行利息计算
1. 实验要求
假设银行Bank()已经有了按整年year计算利息得一般方法,其中year只能取正整数。比如按整年计算得方法:
double puterInterest() {
interest=year*0、35*savedMoney;
return interest;
}
建设银行ConstructionBank就是Bank得子类,准备隐藏继承得成员变量year,并重写计算利息得方法,即自己声明一个double型得year变量,比如,当year取值5、216时,表示要计算5年零216天得利息,但希望首先按银行Bank得方法puterInterest()计算出5整年得利息,然后再计算216天得利息。那么,建设银行就必须把5、216得整数部分赋给隐藏得year,并让super调用隐藏得,按整年计算利息得方法.
要求ConstructionBank与BankOfDalian类就是Bank类得子类,ConstructionBank与BankOfDalian都使用super调用隐藏得成员变量与方法。
2。程序模板
Bank、java
public class Bank {
int savedMoney;
int year;
double interest;
double interestRate = 0、29;
public double puterInterest() {
interest=year*interestRate*savedMoney;
return interest;
}
public void setInterestRate(double rate) {
interestRate = rate;
}
}
-——----——--—————--——----——----—--—-——--—-———--—---—-——--——-——---———----—-----—-———--—-—-----————-——-——---
ConstructionBank、java
public class ConstructionBank extends Bank {
double year;
public double puterInterest() {
super、year=(int)year;
double r = year-(int)year;
int day=(int)(r*1000);
double yearInterest = 【代码1】 //super调用隐藏得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、java
public class BankOfDalian extends Bank {
double year;
public double puterInterest() {
super、year=(int)year;
double r = year-(int)year;
int day=(int)(r*1000);
double yearInterest = 【代码2】// super调用隐藏得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;
}
}
--—---——-—----—----------——---———-——----—----—---—--———-—----——--—---—-——------—-—-—-——-—-——--——-—--—---
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、puterInterest();
BankOfDalian bank2 = new BankOfDalian();
bank2、savedMoney = amount;
bank2、year = 8、236;
bank2、setInterestRate(0、035);
double interest2=bank2、puterInterest();
System、out、printf(”两个银行利息相差%f元\n",interest2—interest1);
}
}
3. 实验扩展
参照建设银行或大连银行,再编写一个商业银行,让程序输出8000元存在商业银行8年零236天得利息
实验3 公司支出得总薪水
1. 实验要求
有一个abstract类,类名为Employee。Employee得子类有YearWorker、MonthWorker与WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按州领取薪水。Employee类有一个abstract方法: Public abstract earnings( ); 子类必须重写父类得earnings( )方法,给出各自领取报酬得具体公式。
有一个pany类,该类用Employee对象数组作为成员,Employee对象数组得单元可以就是YearWorker对象得向上转型对象、MonthWorker对象得向上转型对象或WeekWorker对象得向上转型对象。程序能输出pany对象一年需要支付得薪水总额。
2。 程序模板
apanySalary、java
abstract 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 pany {
Employee[] employee;
double salaries=0;
pany(Employee[] employee) {
this、employee=employee;
}
public double salariesPay() {
salaries=0;
【代码4】 //计算salaries。
return salaries;
}
}
public class panySalary {
public static void main(String args[]) {
Employee [] employee=new Employee[29]; //公司有29名雇员
for(int i=0;i<employee、length;i++) { //雇员简单地分成三类
if(i%3==0)
employee[i]=new WeekWorker();
else if(i%3==1)
employee[i]=new MonthWorker();
else if(i%3==2)
employee[i]=new YearWorker();
}
pany pany=new pany(employee);
System、out、println("公司薪水总额:”+pany、salariesPay()+"元”);
}
}
3、实验指导
对于【代码2】,一年按12个月计算出雇员得一年得年薪,比如:
public double earnings( ) {
return 12*2300;
}
尽管abstract类不能创建对象,但abstract类声明得对象可以存放子类对象得引用,即成为子类对象得向上转型对象。由于abstract类可以有abstract方法,这样就保证子类必须要重写这些abstract方法。由于数组employee得每个段元都就是某个子类对象得上转型对象,实验中得【代码4】可以通过循环语句让数组employee得每个单元调用earnings()方法,并将该方法返回得值累加到salaries,如下所示:
for(int i=0; i<employee、length; i++) {
salaries = salaries+employee[i]、earnings();
}
4.扩展练习
n 子类YearWorker如果不重写earnings()方法,程序编译时提示怎样得错误。
n 再增加一种雇员,并计算公司一年得总薪水。
ﻩ
展开阅读全文