Problem A: 实验四:计算重量

Problem Description

1.实验目的
    (1) 熟悉接口的声明、创建、使用
    (2) 能利用接口的思想解决一般问题
 
2.实验内容 
   有一个ComputerWeight接口,该接口有一个方法:
            public double computeWeight()
   有三个实现该接口的类Television、Computer、WashMachine,这三个类通过实现接口ComputerWeight,计算自身的重量,其中Television重量为45.5,Computer重量为65.5,WashMachine重量为145
有一个Car类,该类用ComputerWeight数组作为成员,ComputerWeight数组的单元可以存放Television、Computer、WashMachine对象的引用,程序能输出Car类对象的总重量。

3.实验要求
   补充完整下面的代码


// 你的代码

public class Main
{
   public static void main(String args[])
   {  ComputerWeight[] goodsOne=new ComputerWeight[50],
       goodsTwo=new ComputerWeight[22] ; 
      for(int i=0;i<goodsOne.length;i++)
       {   if(i%3==0)
             goodsOne[i]=new Television();
           else if(i%3==1)
             goodsOne[i]=new Computer();
           else if(i%3==2)
             goodsOne[i]=new WashMachine();
       } 
     for(int i=0;i<goodsTwo.length;i++)
       {   if(i%3==0)
             goodsTwo[i]=new Television();
           else if(i%3==1)
             goodsTwo[i]=new Computer();
           else if(i%3==2)
             goodsTwo[i]=new WashMachine();
       } 
     Car  largeTruck=new Car(goodsOne);
     System.out.println("The weight of the goods loaded in the large truck: "+largeTruck.getTotalWeights());
     Car  pickup=new Car(goodsTwo);
     System.out.println("The weight of the goods loaded in the pickup: "+pickup.getTotalWeights());
   }
}
 

 

 Output Description

 The weight of the goods loaded in the large truck: 4207.0
The weight of the goods loaded in the pickup: 1837.5

 我的代码:


interface ComputerWeight {
    public double ComputeWeight();

}

class Television implements ComputerWeight {

    @Override
    public double ComputeWeight() {
        return 45.5;
    }
}

class Computer implements ComputerWeight {


    @Override
    public double ComputeWeight() {
        return 65.5;
    }
}

class WashMachine implements ComputerWeight {


    @Override
    public double ComputeWeight() {
        return 145;
    }
}

class Car {
    ComputerWeight[] goods;
    public Car(ComputerWeight[] goodsOne) {
        this.goods = goodsOne;
    }

    public double getTotalWeights() {
        double num = 0;
        for (ComputerWeight good : this.goods) {
            num = num + good.ComputeWeight();
        }

        return num;
    }
}

public class Main {
    public static void main(String args[]) {
        ComputerWeight[] goodsOne = new ComputerWeight[50], goodsTwo = new ComputerWeight[22];
        for (int i = 0; i < goodsOne.length; i++) {
            if (i % 3 == 0)
                goodsOne[i] = new Television();
            else if (i % 3 == 1)
                goodsOne[i] = new Computer();
            else if (i % 3 == 2)
                goodsOne[i] = new WashMachine();
        }
        for (int i = 0; i < goodsTwo.length; i++) {
            if (i % 3 == 0)
                goodsTwo[i] = new Television();
            else if (i % 3 == 1)
                goodsTwo[i] = new Computer();
            else if (i % 3 == 2)
                goodsTwo[i] = new WashMachine();
        }
        Car largeTruck = new Car(goodsOne);
        System.out.println("The weight of the goods loaded in the large truck: " + largeTruck.getTotalWeights());
        Car pickup = new Car(goodsTwo);
        System.out.println("The weight of the goods loaded in the pickup: " + pickup.getTotalWeights());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会编程的七七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值