Java2程序设计实验六

实验二:货车装载重量

package zjq;

interface ComputerWeight{

  public double computeWeight();

}

class Television implements ComputerWeight{

    public double computeWeight(){

        return 3.5;

    }

}



class Computer implements ComputerWeight{

    public double computeWeight()

    {

        return 2.67;

    }

}

class WashMachine implements ComputerWeight{

    public double computeWeight(){

        return 13.8;

    }

}

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(int i=0;i<goods.length;i++)

  {

    totalWeights +=goods[i].computeWeight();

  }

return totalWeights;

  }

}

    public class CheckCarWeight{

  public static void main(String args[]){

    ComputerWeight[]goods=new ComputerWeight[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];

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.5fkg\n",truck.getTotalweights());

  }

}

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糖醋小凉皮

求一点点生活费

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

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

打赏作者

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

抵扣说明:

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

余额充值