JAVA程序设计实验8

一. 填空题(共3题,100分)

1. (填空题)

类实现接口

体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,

而学校考查一个班级的某科目的考试情况时,是计算全班同学的平均成绩。

Gymnastics类和School类都实现了

ComputeAverage接口,但实现的方式不同。请把【代码1】至【代码3】的语句补充完整。(注意:该程序在eclipse中编译及执行)

程序模板如下:

interface ComputeAverage {

   public double average(double x[]);

}

class Gymnastics implements ComputeAverage {

   public double average(double x[]) {

      int count=x.length;

      double aver=0,temp=0;

      for(int i=0;i<count;i++) {

         for(int j=i;j<count;j++) {

            if(x[j]<x[i]) {

              temp=x[j];

              x[j]=x[i];

              x[i]=temp;  

            } 

         }

      }

      for(int i=1;i<count-1;i++) {

         aver=aver+x[i];

      }

      if(count>2)

         aver=aver/(count-2);

      else

         aver=0;

      return aver;

   }

}

class School implements ComputeAverage {//重写public double average(double x[]);返回数组x[]的元素的算术平均

public double average(double x[]) {

double aver=0;

for(double i:x) {

  【代码1】//重写public double average(double x[])方法,返回数组x[]的元素的算术平均,注意使用复合赋值运算符。

}

return aver/x.length;

}

   

}

public class 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[] ={89,56,78,90,100,77,56,45,36,79,98};  

      ComputeAverage computer;

      computer=new Gymnastics();  

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

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

      System.out.printf("体操选手最后得分:%5.3f\n",result);

      computer=new School();  

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

      System.out.printf("班级考试平均分数:%-5.2f",result);

   } 

}

程序运行结果如下:

回答以下问题:

(1)School类如果不重写public double average(double x[])方法,程序编译时是否会提示错误_____________(回答是或者否)? 

(2) 接口ComputeAverage中的public double average(double x[])方法是否能去掉public_____________(回答是或者否)? 

(3)School类中的public double average(double x[])方法是否能去掉public_____________(回答是或者否)?

(4)School类重写public double average(double x[])方法时,是否能够更改average方法类型为float,访问权限更改为protected,

即protected float average(double x[])_____________(回答是或者否)?

(1) aver +=i;

(2) computer.average(a);

(3) computer.average(b);

(4) 是

(5) 是

(6) 否

(7) 否

2. (填空题)

接口回调

货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机,其中一台电视的重量:12kg,一台计算机的重量:8kg,一台洗衣机的重量:20kg。卡车需要计算出整批货物的重量。

具体要求如下:

要求有一个ComputerWeight接口,该接口中有一个方法:  public double computeWeight()

有三个实现该接口的类:Television、Computer和WashMachine,这三个类通过实现接口ComputerWeight给出自重。

然后定义一个 Truck类,该类用ComputerWeight接口类型的数组作为成员( Truck类面向接口),那么该数组的单元就

可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Truck对象所装载的货物的总重量。请把【代码1】至【代码4】的语句补充完整。(注意:该程序在eclipse中编译及执行)

程序模板如下:

interface ComputerWeight {

    public double computeWeight();

}

class Television implements ComputerWeight {

   

 public double computeWeight() {

 【代码1】 //重写computeWeight()方法,返回一台电视的重量。

}

}

class Computer implements ComputerWeight {

 public double computeWeight() {

  【代码2】 //重写computeWeight()方法,返回一台计算机的重量。

}

   

}  

class WashMachine implements ComputerWeight {

    

public double computeWeight() {

   【代码3】 //重写computeWeight()方法,返回一台洗衣机的重量。

}

}

class Truck {

   ComputerWeight [] goods;

   double totalWeights=0;

   Truck(ComputerWeight[] goods) {

       this.goods=goods;

   }

   public void setGoods(ComputerWeight[] goods) {

       this.goods=goods;

   }

   public double getTotalWeights() {

      totalWeights=0;

     

 for(     【代码4】      i:goods) {    //计算totalWeights

         【代码5】 //计算totalWeights,注意使用复合赋值运算符。

    }

   

      return totalWeights;

   }    

}

public class CheckCarWeight {

   public static void main(String args[]) {

      ComputerWeight[] goods=new ComputerWeight[650]; //650件货物

      for(int i=0;i<goods.length;i++) { //简单分成三类

           if(i%3==0)

             goods[i]=new Television();

           else if(i%3==1)

             goods[i]=new Computer();

           else if(i%3==2)

             goods[i]=new WashMachine();

     } 

     Truck truck=new Truck(goods);

     System.out.printf("\n货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());

     goods=new ComputerWeight[68]; //68件货物

     for(int i=0;i<goods.length;i++) { //简单分成两类

          if(i%2==0)

            goods[i]=new Television();

          else 

            goods[i]=new WashMachine();

     } 

     truck.setGoods(goods);

     System.out.printf("货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());

   }

}

程序执行结果如下:

回答以下问题:

(1)当系统增加一种货物如Refrigerator,Truck类是否需要进行修改_____________(回答是或者否)?

(1) return 12;

(2) return 8;

(3)  return 20;

(4) ComputerWeight

(5) totalWeights+=i.computeWeight();

(6) 否

3. (填空题)

面向接口编程

小狗在不同的环境条件下可能呈现不同的状态表现,要求用接口封装小狗的状态。

具体要求如下:

(1)编写一个接口DogState,该接口中有一个方法public void showState()。

(2)编写Dog类,该类中有一个接口DogState声明的变量state,另外,该类中有一个show()

方法,在该方法中让接口state回调showState()方法。

(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。

(4)编写主类,在主类中测试小狗的各种状态。

请把【代码1】至【代码4】的语句补充完整。(注意:该程序在eclipse中编译及执行)

程序模板如下:

interface DogState {

    public void showState();

}

class SoftlyState implements DogState {

    public void showState() {

       System.out.println("听主人的命令");//重写public void showState()方法

    }

}

class MeetEnemyState implements DogState {

   public void showState() {

       System.out.println("狂叫,并冲向去狠咬敌人");

    }  //重写public void showState()方法

}  

class MeetFriendState implements DogState {

   public void showState() {

       System.out.println("晃动尾巴,表示欢迎");

    } //重写public void showState()方法

}

class MeetAnotherDog implements DogState {

   public void showState() {

       System.out.println("嬉戏");

    }//重写public void showState()方法

}

class Dog {

   DogState  state;

   public void show() {

      state.showState();

   }

   public void setState(DogState s) {

      state = s;

   }

}

public class CheckDogState {

   public static void main(String args[]) {

      Dog yellowDog =new Dog();

      System.out.print("狗在主人面前:");

      【代码1】// 设置狗在主人面前应该具备的状态。

      yellowDog.show();

      System.out.print("狗遇到敌人:");

      【代码2】 //设置狗遇到敌人应该具备的状态。

      yellowDog.show();

      System.out.print("狗遇到朋友:");

      【代码3】// 设置狗遇到朋友应该具备的状态。

      yellowDog.show(); 

      System.out.print("狗遇到同伴:");

      【代码4】 //设置狗遇到同伴应该具备的状态。

      yellowDog.show(); 

   }

}

程序执行结果如下:

回答以下问题:

(1)当增加一个实现DogState接口的类(用于刻画小狗的某种状态)后,Dog类是否需要进行修改_____________(回答是或者否)?

(1) yellowDog.setState(new SoftlyState());

(2) yellowDog.setState(new MeetEnemyState());

(3) yellowDog.setState(new MeetFriendState());

(4) yellowDog.setState(new MeetAnotherDog());

(5) 否

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值