• 领导讲话
  • 自我介绍
  • 党会党课
  • 文秘知识
  • 转正申请
  • 问题清单
  • 动员大会
  • 年终总结
  • 工作总结
  • 思想汇报
  • 实践报告
  • 工作汇报
  • 心得体会
  • 研讨交流
  • 述职报告
  • 工作方案
  • 政府报告
  • 调研报告
  • 自查报告
  • 实验报告
  • 计划规划
  • 申报材料
  • 当前位置: 勤学考试网 > 公文文档 > 实验报告 > 正文

    2020年新版第八次实验报告x

    时间:2020-10-17 01:28:57 来源:勤学考试网 本文已影响 勤学考试网手机站

    第八次实验

    实验1中国人、北京人和美国人

    1?实验要求:

    编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、

    ChinaPeople、AmericanPeople 禾口 BeijingPeople 类。要求如下:

    People 类有权限是 protected 的 double 型成员变量 height 和 weight,以及 public void speakHello ( )、public void averageHeight () 禾口 public void averageWeight ()方法。

    ChinaPeople 类是 People 的子类,新增了 public void averageHeight ()和 public voidaverageWeight ()方法。

    AmericanPeople 类是 People 的子类,新增方法 public void AmericanBoxing ()。

    要求 AmericanPeople 重写父类的 public void speakHello ()、public void

    averageHeight ()禾口 public void averageWeight () 方法。

    BeijingPeople 类是 ChinaPeople 的子类,新增 public void beijingOpera ()方

    法。

    2?实验代码:

    //People.java

    public class People {

    protected double weight,height;

    public void speakHello。{

    System.out.pri ntl n("yayayaya");

    }

    public void averageHeight() {

    height=173; System.out.println("average height:"+height);

    }

    public void averageWeight() {

    weight=70; System.out.pri ntl n("average weight:"+weight);

    }

    }

    //Chi naPeople.java

    public class Chin aPeople exte nds People {

    public void speakHello() {

    System.out.println(” 您好");

    }

    public void averageHeight() {

    height=168.78; System.out.println(” 中国人的平均身高: "+height+"厘米");

    }

    public void averageWeight() {

    weight=65;

    System.out.println(” 中国人的平均体重: "+weight+"千克”);

    }

    public void chinaGon gfu() { System.out.println(”坐如钟,站如松,睡如弓 ”);

    }

    }

    //America nPeople.java

    public class America nPeople exte nds People {

    public void speakHello () {

    System.out.pri ntln ("How do you do");

    }

    public void averageHeight() {

    height=176;

    System.out.println("American's average height:"+height+" 厘米"); }

    public void averageWeight() {

    weight=75;

    System.out.println("American's average weight:"+weight+" kg");

    }

    public void america nBox in g() {

    System.out.println(” 直拳,勾拳,组合拳 ”);

    }

    }

    //Beiji ngPeople.java

    public class Beiji ngPeople exte nds Chin aPeople {

    public void averageHeight() {

    height=172.5;

    System.out.println(” 北京人的平均身高:"+height+"厘米");

    }

    public void averageWeight() {

    weight=70;

    System.out.println("北京人得平均体重: "+weight+"千克");

    }

    public void beiji ngOpera() {

    System.out.println(”花脸、青衣、花旦和老生 ”);

    }

    }

    //Example.java

    public class Example {

    public static void main(String arg[]) {

    方法是实例方法,重写时不可以用

    方法是实例方法,重写时不可以用 static修饰。

    方法是实例方法,重写时不可以用

    方法是实例方法,重写时不可以用 static修饰。

    Chin aPeople chi naPeople=new Chin aPeople();

    America nPeople america nPeople=new America nPeople();

    BeijingPeople beijingPeople=new BeijingPeople();

    chin aPeople.speakHello();

    america nPeople.speakHello();

    beiji ngPeople.speakHello();

    chin aPeople.averageHeight();

    america nPeople.averageHeight();

    beiji ngPeople.averageHeight();

    chin aPeople.averageWeight();

    america nPeople.averageWeight();

    beiji ngPeople.averageWeight();

    chin aPeople.ch in aGo ngfu();

    america nPeople.america nBox in g();

    beiji ngPeople.beiji ngOpera();

    beiji ngPeople.ch inaGon gfu();

    }

    }

    3.实验结果:

    4.实验分析:

    (1)方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。

     这样,子类继承的方法才能被隐藏。

    ⑵ 子类在重写方法时,如果重写的方法是 static方法,static关键字必须保留;如果重写的

    }

    }

    }

    (3)如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法 可以改变父类的具遗体行为。

    5?实验后的练习:

    People类中的

    public void speakHello()

    public void averageHeight()

    public void averageWeight()

    三个方法的方法体中的语句是否可以省略。

     答:可以省略,因为省略后结果没有变化

    实验2:银行计算利息

    1?实验要求:

    假设银行bank已经有了按整年year计算利息的一般方法,其中 year只能取正整数。比如,

    按整年计算的方法:

    Double computeri ntern et(){

    In terest=year*0.35*save Mon ey;

    Return interest;

    }

    建设银行constructionBank是bankde子类,准备隐藏继承的成员变量 year,并重写计算利

    息的方法,即自己声明一个 double型的year变量。要求constructionbank和bankofDalian类 是bank类的子类,constructionbank和bankofdalian都使用super调用隐藏的按整年计算利息 的方法。

    2.实验代码:

    //Ban k.java

    public class Bank{

    int savedM on ey;

    int year;

    double in terest;

    double in terestRate=0.29;

    public double computerI nterest(){

    in terest=year*i nterestRate*savedM on ey;

    return in terest;

    }

    public void setI nterestRate( double rate){

    in terestRate=rate;

    }

    }

    // Con structio nBan k.java

    public class Con struct ionBank exte nds Bank{

    double year;

    public double computerI nterest(){

    super.year=(i nt)year;

    double r=year-(i nt)year;

    int day=(i nt)(r*1000);

    double yearI nterest=puterl nterest();

    double dayI nterest=day*0.0001*savedMo ney;

    in terest=yearI nterest+day In terest;

    System.out.printf("%d 元存在建设银行%d 年零 %d 天的禾U息:%f 元

    \n ",savedM on ey,super.year,day,i nterest);

    return in terest;

    }

    }

    // Ban kOfDalia n.java

    public class Ban kOfDalia n exte nds Bank {

    double year;

    public double computerI nterest(){

    super.year=(i nt)year;

    double r=year-(i nt)year;

    int day=(i nt)(r*1000);

    double yearI nterest=puterl nterest();

    double day In terest=day*0.00012*savedMo ney;

    in terest=yearI nterest+day In terest;

    System.out.printf("%d 元存在大连银行 %d 年零 %d 天的禾U息:%f 元 \n ",savedM on ey,super.year,day,i nterest);

    return in terest;

    // SaveMon ey.java

    public class SaveMon ey{

    public static void main(String args[]){

    int amou nt=8000;

    Con struct ionBank ban k1= new Con struct ionBan k();

    ban k1.savedM on ey=am ount;

    ban k1.year=8.236;

    ba nk1.setI nterestRate(0.035);

    double in terest1=ba puterl nterest();

    Ban kOfDalian ban k2=new Ban kOfDalia n();

    ban k2.savedM on ey=am ount;

    ban k2.year=8.236;

    ba nk2.setI nterestRate(0.035);

    double in terest2=ba puterl nterest();

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

    }

    3.实验结果:

    4.实验分析:

    子类不继承父类的构造方法,因此子类在其构造方法中需使用 super来调用父类的

    构造方法,并且super必须是子类构造方法中的头一条语句。

    当super调用被隐藏的方法时,该方法中出现的成员变量是被子类隐藏的成员变量 或继承的成员变量。

    5.实验后的练习:

    }

    }

    }

    }

    }

    }

    }

    }

    }

    参照建设银行或大连银行,在编写一个商业银行,让程序输出 8000元存在商业银行8年零

    236天的利息。

    //Ban k.java

    public class Bank{

    int savedM on ey;

    int year;

    double in terest;

    double in terestRate=0.29;

    public double computerI nterest(){

    in terest=year*i nterestRate*savedM on ey;

    return in terest;

    }

    public void setI nterestRate( double rate){

    in terestRate=rate;

    }

    }

    // CommercialBa nk

    public class CommercialBa nk exte nds Bank {

    double year;

    public double computerI nterest(){

    super.year=(i nt)year;

    double r=year-(i nt)year;

    int day=(i nt)(r*1000);

    double yearI nterest=puterl nterest();

    double day In terest=day*0.00012*savedMo ney;

    in terest=yearI nterest+day In terest;

    System.out.printf("%d 元存在商业银行 %d 年零 %d 天的利息:%f 元

    \n ",savedM on ey,super.year,day,i nterest);

    return in terest;

    }

    }

    // SaveMon ey.java

    public class SaveMon ey{

    public static void main(String args[]){

    int amou nt=8000;

    CommercialBa nk bank=new CommercialBa nk();

    ban k.savedM on ey=am ount;

    ban k.year=8.236;

    ban k.set In terestRate(0.035);

    double in terest=ba puterl nterest();

    *1

    *1

    实验3:公司支出的总薪水

    1?实验要求:

    要求有一个 abstract类,类名为 Employee。Employee 的子类有 YearWorker、MonthWorker、 WeekWorker。YearWorker对象按年领取薪水, MonthWorker按月领取薪水、 WeekWorker按 周领取的薪水。

     Employee类有一个abstract方法:

    public abstract earnin gs();

    子类必须重写父类的 earin gs()方法,给出各自领取报酬的具体方式。

    有一个Company类,该类用 Employee对象数组作为成员, Employee对象数组的单元可以 是YearWorker对象的上转型对象、 MonthWorker独享的上转型独享或 weekworker对象的上 转型独享。程序能输出 Company对象一年需要支付的薪水总额。

    2?实验代码:

    abstract class Employee{

    public abstract double earnin gs();

    }

    class YearWorker exte nds Employee{

    public double earnin gs(){

    return 12000;

    }

    }

    class Mon thWorker exte nds Employee{

    public double earnin gs(){

    return 12*2300;

    }

    }

    class WeekWorker exte nds Employee{

    public double earnin gs(){

    return 52*780;

    }

    }

    class Compa ny{

    Employee]] employee;

    double salaries=0;

    Compa ny (Employee]] employee){

    this.employee=employee;

    }

    public double salariesPay(){

    salaries=0;

    for(i nt i=0;i<employee」en gth;i++){ salaries=salaries+employee[i].earnin gs();

    }

    retur n salaries;

    }

    }

    public class Compa ny Salary{

    public static void main(String args[]){

    Employee]] employee=new Employee[29];

    for(i nt i=0;i<employee .len gth;i++){

    if(i%3==0)

    employee[i]=new WeekWorker();

    else if(i%3==1)

    employee[i]=n ew Mon thWorker();

    else if(i%3==2)

    employee[i]=new YearWorker();

    }

    Compa ny compa ny=new Compa ny (employee);

    System.out.println("公司薪水总额:"+company.salariesPay()+"元"); }

    }

    3.实验结果:

    恪司 薪 人总额:阳帕财血:正 lPi*ess an^! keys tzu cant inue --

    *1

    4?实验分析:

    尽管abstract类不能创建对象,但 abstract类声明的对象可以存放子类对象的引用,即成

    为子类对象的上转型对象。

    5?实验后的练习:

    子类yearworker如果不重写earnings ()方法,程序编译是提示怎么样的错误。

    YearWorker不是抽象的,并且未覆盖 Employee中的抽象方法 earnings()

    class YearWorker exte nds Employee{

    在增加一种雇员,并计算公司一年的总薪水。

    class DayWorker exte nds Employee{

    public double earnin gs(){

    return 365*100;

    }

    }

    将for语句改写为:

    for(i nt i=0;i<employee」en gth;i++){

    if(i%4==0)

    employee"]=new WeekWorker();

    else if(i%4==1)

    employee[i]=n ew Mon thWorker();

    else if(i%4==2) employee[i]=new YearWorker();

    else if(i%4==3)

    employee[i]=new DayWorker();

    • 考试时间
    • 范文大全
    • 作文大全
    • 课程
    • 试题
    • 招聘
    • 文档大全

    推荐访问