实验项目2 第2部分 继承与接口.docx

上传人:b****2 文档编号:23117663 上传时间:2023-04-30 格式:DOCX 页数:17 大小:18.03KB
下载 相关 举报
实验项目2 第2部分 继承与接口.docx_第1页
第1页 / 共17页
实验项目2 第2部分 继承与接口.docx_第2页
第2页 / 共17页
实验项目2 第2部分 继承与接口.docx_第3页
第3页 / 共17页
实验项目2 第2部分 继承与接口.docx_第4页
第4页 / 共17页
实验项目2 第2部分 继承与接口.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

实验项目2 第2部分 继承与接口.docx

《实验项目2 第2部分 继承与接口.docx》由会员分享,可在线阅读,更多相关《实验项目2 第2部分 继承与接口.docx(17页珍藏版)》请在冰豆网上搜索。

实验项目2 第2部分 继承与接口.docx

实验项目2第2部分继承与接口

实验项目2Java面向对象程序设计

第2部分继承与接口

[实验目的]

1、掌握java继承中父类及其子类的定义方法。

2、掌握子类重写父类同名方法的方法。

3、掌握接口的用法。

[实验要求]

1、复习理论教学中所学的内容。

2、认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。

3、认真总结实验并书写实验报告。

[实验课时]2学时

[实验教学方式]学生上机实验,教师随堂指导。

[实验内容]

1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。

最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。

该程序的模板代码如下:

请将其补充完整并调试运行。

classPeople

{

protecteddoubleweight,height;

publicvoidspeakHello()

{

System.out.println("yayawawa");

}

publicvoidaverageHeight()

{

height=173;

System.out.println("averageheight:

"+height);

}

publicvoidaverageWeight()

{

weight=70;

System.out.println("averageweight:

"+weight);

}

}

classChinaPeopleextendsPeople

{

【代码1】//重写publicvoidspeakHello()方法,要求输出类似“你好,吃了吗”这样的

//汉语信息

【代码2】//重写publicvoidaverageHeight()方法,要求输出类似

//“中国人的平均身高:

168.78厘米”这样的汉语信息

【代码3】//重写publicvoidaverageWeight()方法,

//要求输出类似“中国人的平均体重:

65公斤”这样的汉语信息

publicvoidchinaGongfu()

{

【代码4】//输出中国武术的信息,例如:

"坐如钟,站如松,睡如弓"等

}

}

classAmericanPeopleextendsPeople

{

【代码5】//重写publicvoidspeakHello()方法,要求输出类似

//“Howdoyoudo”这样的英语信息。

【代码6】//重写publicvoidaverageHeight()方法

【代码7】//重写publicvoidaverageWeight()方法

publicvoidamericanBoxing()

{

【代码8】//输出拳击的信息,例如,“直拳”、“钩拳”等

}

}

classBeijingPeopleextendsChinaPeople

{

【代码9】//重写publicvoidspeakHello()方法,要求输出类似“您好”这样的汉语信息

【代码10】//重写publicvoidaverageHeight()方法

【代码11】//重写publicvoidaverageWeight()方法

publicvoidbeijingOpera()

{

【代码12】//输出京剧的信息

}

}

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、读懂下面模板代码,按要求补充程序并调试运行。

掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。

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;

}

}

publicclassHardWork

{

publicstaticvoidmain(Stringargs[])

{

Employee[]employee=newEmployee[20];

for(inti=0;i

{

if(i%3==0)

employee[i]=newWeekWorker();

elseif(i%3==1)

employee[i]=newMonthWorker();

elseif(i%3==2)

employee[i]=newYearWorker();

}

Companycompany=newCompany(employee);

System.out.println("公司年工资总额:

"+company.salariesPay());

}

}

3、读懂下面模板代码,按要求补充程序并调试运行。

掌握接口的定义及其实现方法,学习接口回调的运用方法。

interfaceComputerWeight

{

publicdoublecomputeWeight();

}

classTelevisionimplementsComputerWeight

{【代码1】//实现computeWeight()方法。

}

classComputerimplementsComputerWeight

{【代码2】//实现computeWeight()方法。

}

classWashMachineimplementsComputerWeight

{【代码3】//实现computeWeight()方法。

}

classCar

{ComputerWeight[]goods;

doubletotalWeights=0;

Car(ComputerWeight[]goods)

{

this.goods=goods;

}

publicdoublegetTotalWeights()

{

totalWeights=0;

【代码4】//计算totalWeights

returntotalWeights;

}

}

publicclassRoad

{

publicstaticvoidmain(Stringargs[])

{ComputerWeight[]goodsOne=newComputerWeight[50],

goodsTwo=newComputerWeight[22];

for(inti=0;i

{if(i%3==0)

goodsOne[i]=newTelevision();

elseif(i%3==1)

goodsOne[i]=newComputer();

elseif(i%3==2)

goodsOne[i]=newWashMachine();

}

for(inti=0;i

{if(i%3==0)

goodsTwo[i]=newTelevision();

elseif(i%3==1)

goodsTwo[i]=newComputer();

elseif(i%3==2)

goodsTwo[i]=newWashMachine();

}

Car大货车=newCar(goodsOne);

System.out.println("大货车装载的货物重量:

"+大货车.getTotalWeights());

Car小货车=newCar(goodsTwo);

System.out.println("小货车装载的货物重量:

"+小货车.getTotalWeights());

}

}

以下实验内容4—7中选做两题。

4.银行与利息

请按模板要求,将【代码】替换为Java程序代码。

//Bank.java

publicclassBank{

intsavedMoney;

intyear;

doubleinterest;

doubleinterestRate=0.29;

publicdoublecomputerInterest(){

interest=year*interestRate*savedMoney;

returninterest;

}

publicvoidsetInterestRate(doublerate){

interestRate=rate;

}

}

//ConstructionBank.java

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;

}

}

//BankOfDalian.java

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;

}

}

//SaveMoney.java

publicclassSaveMoney{

publicstaticvoidmain(Stringargs[]){

intamount=8000;

ConstructionBankbank1=newConstructionBank();

bank1.savedMoney=amount;

bank1.year=8.236;

bank1.setInterestRate(0.035);

doubleinterest1=puterInterest();

BankOfDalianbank2=newBankOfDalian();

bank2.savedMoney=amount;

bank2.year=8.236;

bank2.setInterestRate(0.035);

doubleinterest2=puterInterest();

System.out.printf("两个银行利息相差%f元\n",interest2-interest1);

}

}

5.面积之和

请按模板要求,将【代码】替换为Java程序代码。

//Geometry.java

publicabstractclassGeometry{

publicabstractdoublegetArea();

}

//TotalArea.java

publicclassTotalArea{

Geometry[]tuxing;

doubletotalArea=0;

publicvoidsetTuxing(Geometry[]t){

tuxing=t;

}

publicdoublecomputerTotalArea(){

【代码3】//用循环语句让tuxing的元素调用getArea方法,并将返回的值累加到totalArea

returntotalArea;

}

}

//Rect.java

publicclassRectextendsGeometry{

doublea,b;

Rect(doublea,doubleb){

this.a=a;

this.b=b;

}

【代码1】//重写getArea()方法

}

//Circle.java

publicclassCircleextendsGeometry{

doubler;

Circle(doubler){

this.r=r;

}

【代码2】//重写getArea()方法

}

//MainClass.java

publicclassMainClass{

publicstaticvoidmain(Stringargs[]){

Geometry[]tuxing=newGeometry[29];//有29个Geometry对象

for(inti=0;i

if(i%2==0)

tuxing[i]=newRect(16+i,68);

elseif(i%2==1)

tuxing[i]=newCircle(10+i);

}

TotalAreacomputer=newTotalArea();

computer.setTuxing(tuxing);

System.out.printf("各种图形的面积之和:

\n%f",puterTotalArea());

}

}

6.歌手大赛

程序模板

请按模板要求,将【代码】替换为Java程序代码。

//CompurerAverage.java

publicinterfaceCompurerAverage{//接口

publicdoubleaverage(doublex[]);

}

//SongGame.java

publicclassSongGameimplementsCompurerAverage{

publicdoubleaverage(doublex[]){

intcount=x.length;

doubleaver=0,temp=0;

for(inti=0;i

for(intj=i;j

if(x[j]

temp=x[j];

x[j]=x[i];

x[i]=temp;

}

}

}

for(inti=1;i

aver=aver+x[i];

}

if(count>2)

aver=aver/(count-2);

else

aver=0;

returnaver;

}

}

//School.java

publicclassSchoolimplementsCompurerAverage{

【代码1】//重写publicdoubleaverage(doublex[])方法,返回数组x[]的元素的算术平均

}

//Estimator.java

publicclassEstimator{//主类

publicstaticvoidmain(Stringargs[]){

doublea[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97};

doubleb[]={56,55.5,65,50,51.5,53.6,70,49,66,62,46};

CompurerAveragecomputer;

computer=newSongGame();

doubleresult=【代码2】//computer调用average(doublex[])方法,将数组a传递给参数x

System.out.printf("%n");

System.out.printf("歌手最后得分:

%5.3f\n",result);

computer=newSchool();

result=【代码3】//computer调用average(doublex[])方法,将数组b传递给参数x

System.out.printf("学生平均体重:

%-5.2fkg",result);

}

}

7.天气预报

请按模板要求,将【代码】替换为Java程序代码。

WeatherState.java

publicinterfaceWeatherState{//接口

publicvoidshowState();

}

Weather.java

publicclassWeather{

WeatherStatestate;

publicvoidshow(){

state.showState();

}

publicvoidsetState(WeatherStates){

state=s;

}

}

WeatherForecast.java

publicclassWeatherForecast{//主类

publicstaticvoidmain(Stringargs[]){

WeatherweatherBeijing=newWeather();

System.out.print("\n今天白天:

");

weatherBeijing.setState(newCloudyDayState());

weatherBeijing.show();

System.out.print("\n今天夜间:

");

weatherBeijing.setState(newLightRainState());

weatherBeijing.show();

System.out.print("转:

");

weatherBeijing.setState(newHeavyRainState());

weatherBeijing.show();

System.out.print("\n明天白天:

");

weatherBeijing.setState(newLightRainState());

weatherBeijing.show();

System.out.print("\n明天夜间:

");

weatherBeijing.setState(newCloudyLittleState());

weatherBeijing.show();

}

}

CloudyLittleState.java

publicclassCloudyLittleStateimplementsWeatherState{

publicvoidshowState(){

System.out.print("少云,有时晴.");

}

}

CloudyDayState.java

publicclassCloudyDayStateimplementsWeatherState{

【代码1】//重写publicvoidshowState()

}

HeavyRainState.java

publicclassHeavyRainStateimplementsWeatherState{

【代码2】//重写publicvoidshowState()

}

LightRainState.java

publicclassLightRainStateimplementsWeatherState{

【代码3】//重写

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 辩护词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1