JAVA实验报告2
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
甘肃政法学院
本科生实验报告 (二)
姓名:俞晓 学院:信息工程学院 专业:信息管理与信息系统 班级:13 级信管班 实验课程名称: 指导教师:李瑞生 教师 实验成绩: 实验日期: 2015 年 4 月 14 日 学期 开课时间:2014-2015 学年 第二
甘肃政法学院实验管理中心印制
实验题目 姓名
// 【代码2】super调用隐藏的computerInterest()方法 double yearInterest = super.computerInterest(); double dayInterest = day*0.00012*savedMoney; interest= yearInterest+dayInterest; System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n", savedMoney,super.year,day,interest); return interest; } }
} } BeijingPeople.java public class BeijingPeople extends ChinaPeople { //重写 public void speakHello()方法,要求输出类似“您好”这样的汉语信息 public void speakHello() { System.out.println("您好啊!");
} } AmericanPeople.java public class AmericanPeople extends People { //重写 public void speakHello()方法 public void speakHello() { System.out.println("How do you do?"); } //重写 public void averageHeight()方法 public void averageHeight() { height=175.78; System.out.println("AmericanPeople average height:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=68; System.out.println("AmericanPeople average weight:"+weight); } public void americanBoxing(){ //输出拳击的信息,例如, “直拳”、 “钩拳”等 System.out.println("直拳、钩拳、摆拳");
//【代码4】 计算totalWeights for ( int i = 0; i < goods.length; i++ ){ totalWeights += goods[i].computeWeight (); } return totalWeights; } } 运行结果:
4. 银行与利息 请按模板要求,将【代码】替换为 Java 程序代码。
主要填写代码为:
People.java public 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("公司年工资总额:"+company.salariesPay()+"元"); } }
运行结果如图所示:
wenku.baidu.com
3、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定 义及其实现方法,学习接口回调的运用方法。
主要填写代码为: class Television implements ComputerWeight{ //【代码1】 实现computeWeight()方法 public double computeWeight() { return 20;
System.out.println("average weight:"+weight); } } ChinaPeople.java public class ChinaPeople extends People { //重写 public void speakHello()方法 public void speakHello() { System.out.println("你好,吃了么?"); } //重写 public void averageHeight()方法 public void averageHeight() { height=168.78; System.out.println("中国人平均身高:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=65; System.out.println("中国人平均体重:"+weight+"公斤"); } //输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等 public void chinaGongfu(){ System.out.println("坐如钟,站如松,睡如弓");
public class HardWork { public static void main(String args[]){ Employee[] employee=new Employee[20]; 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(); } Company company=new Company(employee);
主要填写代码为:
public class ConstructionBank extends Bank { double year; public double computerInterest() { super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000); //【代码1】super调用隐藏的computerInterest()方法 double yearInterest = 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; } } public class BankOfDalian extends Bank { double year; public double computerInterest() { super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000);
} //重写 public void averageHeight()方法 public void averageHeight() { height=163.78; System.out.println("北京人平均身高:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=63; System.out.println("北京人平均体重:"+weight+"公斤");
} } class Computer implements ComputerWeight{ //【代码2】 实现computeWeight()方法。 public double computeWeight() { return 40; } } class WashMachine implements ComputerWeight{ // 【代码3】 实现computeWeight()方法。 public double computeWeight() { return 30; } } class Car{ ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods){ this.goods=goods; } public double getTotalWeights(){ totalWeights=0;
继承与接口 俞晓 班级 13 级信管 学 班
小组合作
否
号 201381020144
一、实验目的 1、掌握 java 继承中父类及其子类的定义方法。 2、掌握子类重写父类同名方法的应用。 3、掌握接口的定义及其使用方法。 4、掌握运用类继承、抽象类、接口解决应用问题的方法。
二.实验环境 Windows7 JDK1.7 Eclipse 三、实验内容与步骤 1、定义父类 People,分别定义 People 类的子类 ChinaPeople, AmericanPeople 和 BeijingPeople 并分别重写父类中的各个方法。 最后在主方法中分别创建各子类的对象并调用各自的方法打印输出 信息。
} public void beijingOpera() { //输出京剧的信息
System.out.println("新贵妃醉酒"); } }
运行结果如图所示:
2.读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定 义及其实现方法,学习上转型对象的运用方法。
主要填写代码为:
abstract class Employee{ public abstract double earnings(); } class YearWorker extends Employee{ //【代码1】 重写earnings()方法 public double earnings() { return 200000; } } class MonthWorker extends Employee{ // 【代码2】 重写earnings()方法。 public double earnings() { return 12*8000; }
} class WeekWorker extends Employee{ //【代码3】重写earnings()方法。 public double earnings() { return 52*1000; } } class Company{ Employee[] employee; double salaries=0; Company(Employee[] employee){ this.employee=employee; } public double salariesPay(){ salaries=0; //【代码4】 计算salaries。 for(int i=0;i<employee.length;i++) { salaries+=employee[i].earnings(); } return salaries; } }
本科生实验报告 (二)
姓名:俞晓 学院:信息工程学院 专业:信息管理与信息系统 班级:13 级信管班 实验课程名称: 指导教师:李瑞生 教师 实验成绩: 实验日期: 2015 年 4 月 14 日 学期 开课时间:2014-2015 学年 第二
甘肃政法学院实验管理中心印制
实验题目 姓名
// 【代码2】super调用隐藏的computerInterest()方法 double yearInterest = super.computerInterest(); double dayInterest = day*0.00012*savedMoney; interest= yearInterest+dayInterest; System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n", savedMoney,super.year,day,interest); return interest; } }
} } BeijingPeople.java public class BeijingPeople extends ChinaPeople { //重写 public void speakHello()方法,要求输出类似“您好”这样的汉语信息 public void speakHello() { System.out.println("您好啊!");
} } AmericanPeople.java public class AmericanPeople extends People { //重写 public void speakHello()方法 public void speakHello() { System.out.println("How do you do?"); } //重写 public void averageHeight()方法 public void averageHeight() { height=175.78; System.out.println("AmericanPeople average height:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=68; System.out.println("AmericanPeople average weight:"+weight); } public void americanBoxing(){ //输出拳击的信息,例如, “直拳”、 “钩拳”等 System.out.println("直拳、钩拳、摆拳");
//【代码4】 计算totalWeights for ( int i = 0; i < goods.length; i++ ){ totalWeights += goods[i].computeWeight (); } return totalWeights; } } 运行结果:
4. 银行与利息 请按模板要求,将【代码】替换为 Java 程序代码。
主要填写代码为:
People.java public 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("公司年工资总额:"+company.salariesPay()+"元"); } }
运行结果如图所示:
wenku.baidu.com
3、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定 义及其实现方法,学习接口回调的运用方法。
主要填写代码为: class Television implements ComputerWeight{ //【代码1】 实现computeWeight()方法 public double computeWeight() { return 20;
System.out.println("average weight:"+weight); } } ChinaPeople.java public class ChinaPeople extends People { //重写 public void speakHello()方法 public void speakHello() { System.out.println("你好,吃了么?"); } //重写 public void averageHeight()方法 public void averageHeight() { height=168.78; System.out.println("中国人平均身高:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=65; System.out.println("中国人平均体重:"+weight+"公斤"); } //输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等 public void chinaGongfu(){ System.out.println("坐如钟,站如松,睡如弓");
public class HardWork { public static void main(String args[]){ Employee[] employee=new Employee[20]; 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(); } Company company=new Company(employee);
主要填写代码为:
public class ConstructionBank extends Bank { double year; public double computerInterest() { super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000); //【代码1】super调用隐藏的computerInterest()方法 double yearInterest = 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; } } public class BankOfDalian extends Bank { double year; public double computerInterest() { super.year=(int)year; double r = year-(int)year; int day=(int)(r*1000);
} //重写 public void averageHeight()方法 public void averageHeight() { height=163.78; System.out.println("北京人平均身高:"+height+"厘米"); } //重写 public void averageWeight()方法 public void averageWeight() { weight=63; System.out.println("北京人平均体重:"+weight+"公斤");
} } class Computer implements ComputerWeight{ //【代码2】 实现computeWeight()方法。 public double computeWeight() { return 40; } } class WashMachine implements ComputerWeight{ // 【代码3】 实现computeWeight()方法。 public double computeWeight() { return 30; } } class Car{ ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods){ this.goods=goods; } public double getTotalWeights(){ totalWeights=0;
继承与接口 俞晓 班级 13 级信管 学 班
小组合作
否
号 201381020144
一、实验目的 1、掌握 java 继承中父类及其子类的定义方法。 2、掌握子类重写父类同名方法的应用。 3、掌握接口的定义及其使用方法。 4、掌握运用类继承、抽象类、接口解决应用问题的方法。
二.实验环境 Windows7 JDK1.7 Eclipse 三、实验内容与步骤 1、定义父类 People,分别定义 People 类的子类 ChinaPeople, AmericanPeople 和 BeijingPeople 并分别重写父类中的各个方法。 最后在主方法中分别创建各子类的对象并调用各自的方法打印输出 信息。
} public void beijingOpera() { //输出京剧的信息
System.out.println("新贵妃醉酒"); } }
运行结果如图所示:
2.读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定 义及其实现方法,学习上转型对象的运用方法。
主要填写代码为:
abstract class Employee{ public abstract double earnings(); } class YearWorker extends Employee{ //【代码1】 重写earnings()方法 public double earnings() { return 200000; } } class MonthWorker extends Employee{ // 【代码2】 重写earnings()方法。 public double earnings() { return 12*8000; }
} class WeekWorker extends Employee{ //【代码3】重写earnings()方法。 public double earnings() { return 52*1000; } } class Company{ Employee[] employee; double salaries=0; Company(Employee[] employee){ this.employee=employee; } public double salariesPay(){ salaries=0; //【代码4】 计算salaries。 for(int i=0;i<employee.length;i++) { salaries+=employee[i].earnings(); } return salaries; } }