




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验项目2 Java面向对象程序设计第2部分 继承与接口实验目的1、掌握java 继承中父类及其子类的定义方法。2、掌握子类重写父类同名方法的方法。3、掌握接口的用法。实验要求1、 复习理论教学中所学的内容。2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。3、 认真总结实验并书写实验报告。实验课时 2学时实验教学方式 学生上机实验,教师随堂指导。实验内容1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。
2、该程序的模板代码如下:请将其补充完整并调试运行。class People protected double weight,height; public void speakHello() System.out.println(yayawawa); public void averageHeight() height=173; System.out.println(average height:+height); public void averageWeight() weight=70; System.out.println(average weight:+weight); class Chi
3、naPeople extends People 【代码1】 /重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的 /汉语信息【代码2】 /重写public void averageHeight()方法,要求输出类似 /“中国人的平均身高:168.78厘米”这样的汉语信息【代码3】 /重写public void averageWeight()方法, /要求输出类似“中国人的平均体重:65公斤”这样的汉语信息 public void chinaGongfu() 【代码4】/输出中国武术的信息,例如:坐如钟,站如松,睡如弓等 class AmericanPe
4、ople extends People【代码5】 /重写public void speakHello()方法,要求输出类似 /“How do you do”这样的英语信息。【代码6】 /重写public void averageHeight()方法 【代码7】 /重写public void averageWeight()方法 public void americanBoxing() 【代码8】/输出拳击的信息,例如,“直拳”、“钩拳”等 class BeijingPeople extends ChinaPeople 【代码9】 /重写public void speakHello()方法,要求输
5、出类似“您好”这样的汉语信息 【代码10】 /重写public void averageHeight()方法 【代码11】 /重写public void averageWeight()方法 public void beijingOpera() 【代码12】/输出京剧的信息 public class Example public static void main(String args) ChinaPeople chinaPeople=new ChinaPeople(); AmericanPeople americanPeople=new AmericanPeople(); BeijingPeo
6、ple beijingPeople=new BeijingPeople(); chinaPeople.speakHello(); americanPeople.speakHello(); beijingPeople.speakHello(); chinaPeople.averageHeight(); americanPeople.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeig
7、ht(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera() ; beijingPeople.chinaGongfu(); 2、读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。abstract class Employee public abstract double earnings();class YearWorker extends Employee 【代码1】 /重写earnings()方法class Mon
8、thWorker extends Employee 【代码2】 /重写earnings()方法。class WeekWorker extends Employee 【代码3】 /重写earnings()方法。class Company Employee employee; double salaries=0; Company(Employee employee) this.employee=employee; public double salariesPay() salaries=0; 【代码4】 /计算salaries。 return salaries; public class Hard
9、Work public static void main(String args) Employee employee=new Employee20; 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(公司年工资总额:
10、+company.salariesPay(); 3、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定义及其实现方法,学习接口回调的运用方法。interface ComputerWeight public double computeWeight();class Television implements ComputerWeight 【代码1】 /实现computeWeight()方法。class Computer implements ComputerWeight 【代码2】 /实现computeWeight()方法。 class WashMachine implements Com
11、puterWeight 【代码3】 /实现computeWeight()方法。class Car ComputerWeight goods; double totalWeights=0; Car(ComputerWeight goods) this.goods=goods; public double getTotalWeights() totalWeights=0; 【代码4】 /计算totalWeights return totalWeights; public class Road public static void main(String args) ComputerWeight g
12、oodsOne=new ComputerWeight50, goodsTwo=new ComputerWeight22 ; for(int i=0;igoodsOne.length;i+) if(i%3=0) goodsOnei=new Television(); else if(i%3=1) goodsOnei=new Computer(); else if(i%3=2) goodsOnei=new WashMachine(); for(int i=0;igoodsTwo.length;i+) if(i%3=0) goodsTwoi=new Television(); else if(i%3
13、=1) goodsTwoi=new Computer(); else if(i%3=2) goodsTwoi=new WashMachine(); Car 大货车=new Car(goodsOne); System.out.println(大货车装载的货物重量:+大货车.getTotalWeights(); Car 小货车=new Car(goodsTwo); System.out.println(小货车装载的货物重量:+小货车.getTotalWeights(); 以下实验内容47中选做两题。4. 银行与利息请按模板要求,将【代码】替换为Java程序代码。/Bank.javapublic c
14、lass 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.javapublic class ConstructionBank extends Bank dou
15、ble year; public double computerInterest() super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000); double yearInterest = 【代码1】 /super调用隐藏的computerInterest()方法 double dayInterest = day*0.0001*savedMoney; interest= yearInterest+dayInterest; System.out.printf(%d元存在建设银行%d年零%d天的利息:%f元n, s
16、avedMoney,super.year,day,interest); return interest; /BankOfDalian.javapublic 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 = 【代码2】/ super调用隐藏的computerInterest()方法 double dayIn
17、terest = day*0.00012*savedMoney; interest= yearInterest+dayInterest; System.out.printf(%d元存在大连银行%d年零%d天的利息:%f元n, savedMoney,super.year,day,interest); return interest; /SaveMoney.javapublic class SaveMoney public static void main(String args) int amount=8000; ConstructionBank bank1 = new Construction
18、Bank(); bank1.savedMoney = amount; bank1.year = 8.236; bank1.setInterestRate(0.035); double interest1 = puterInterest(); BankOfDalian bank2 = new BankOfDalian(); bank2.savedMoney = amount; bank2.year = 8.236; bank2.setInterestRate(0.035); double interest2=puterInterest(); System.out.printf(两个银行利息相差%
19、f元n,interest2-interest1); 5.面积之和请按模板要求,将【代码】替换为Java程序代码。/Geometry.javapublic abstract class Geometry public abstract double getArea();/TotalArea.javapublic class TotalArea Geometry tuxing; double totalArea=0; public void setTuxing(Geometry t) tuxing=t; public double computerTotalArea() 【代码3】/用循环语句让t
20、uxing的元素调用getArea方法,并将返回的值累加到totalArea return totalArea; /Rect.javapublic class Rect extends Geometry double a,b; Rect(double a,double b) this.a = a; this.b = b; 【代码1】 /重写 getArea()方法/Circle.javapublic class Circle extends Geometry double r; Circle(double r) this.r = r; 【代码2】 /重写 getArea()方法/MainCla
21、ss.javapublic class MainClass public static void main(String args) Geometry tuxing=new Geometry29; /有29个Geometry对象 for(int i=0;ituxing.length;i+) /29个Geometry对象分成两类 if(i%2=0) tuxingi=new Rect(16+i,68); else if(i%2=1) tuxingi=new Circle(10+i); TotalArea computer=new TotalArea(); computer.setTuxing(tu
22、xing); System.out.printf(各种图形的面积之和:n%f,puterTotalArea(); 6.歌手大赛程序模板请按模板要求,将【代码】替换为Java程序代码。/CompurerAverage.javapublic interface CompurerAverage /接口 public double average(double x);/SongGame.javapublic class SongGame implements CompurerAverage public double average(double x) int count=x.length; doub
23、le aver=0,temp=0; for(int i=0;icount;i+) for(int j=i;jcount;j+) if(xjxi) temp=xj; xj=xi; xi=temp; for(int i=1;i2) aver=aver/(count-2); else aver=0; return aver; /School.javapublic class School implements CompurerAverage 【代码1】/重写public double average(double x)方法,返回数组x的元素的算术平均/Estimator.javapublic cla
24、ss Estimator /主类 public static void main(String args) double a = 9.89,9.88,9.99,9.12,9.69,9.76,8.97; double b =56,55.5,65,50,51.5,53.6,70,49,66,62,46; CompurerAverage computer; computer=new SongGame(); double result=【代码2】 /computer调用average(double x)方法,将数组a传递给参数x System.out.printf(%n); System.out.pr
25、intf(歌手最后得分:%5.3fn,result); computer=new School(); result=【代码3】 /computer调用average(double x)方法,将数组b传递给参数x System.out.printf(学生平均体重:%-5.2f kg,result); 7.天气预报请按模板要求,将【代码】替换为Java程序代码。WeatherState.javapublic interface WeatherState /接口 public void showState();Weather.javapublic class Weather WeatherState state; public void show() state.showState(); public void setState(WeatherState s) state = s; WeatherForecast.javapublic class WeatherForecast /主类 public static void main(String args) Weather weatherBeijing =new Weather(); System.out.print(n今天白天:); weatherBeijing.setState(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公示牌安装合同协议书
- 陈列活动方案协议书
- 拔出指甲治疗
- 邮政挂号寄递协议书
- 高空吊装免责协议书
- 儿童俱乐部安全协议书
- 交行信用卡减免协议书
- csc留学资助协议书
- 农村搬迁房转让协议书
- 饭店着火赔偿协议书
- 肥胖症诊疗指南(2024年版)解读
- 麦收消防安全培训课件
- 《科普技巧常识》课件
- 2025年中国全电脑横机市场现状分析及前景预测报告
- 大型活动场馆停车管理方案与技术措施
- 医院基建管理试题及答案
- 2025年全国保密教育线上培训考试试题库及答案(夺冠)带答案详解
- 沪教牛津版(深圳用)英语五年级下册Unit-11-Chinese-festivals课件
- DBJ50-T-078-2016重庆市城市道路工程施工质量验收规范
- MOOC 跨文化交际通识通论-扬州大学 中国大学慕课答案
- GA 1283-2015住宅物业消防安全管理
评论
0/150
提交评论