卡车载重
分数 20
全屏浏览
切换布局
作者 clk
单位 浙江工商大学杭州商学院
某大型家电企业拥有一批送货卡车,运送电视机、空调、洗衣机等家电。编程计算每个卡车所装载货物的总重量。
要求如下:
1.创建一个ComputeWeight接口,接口中有方法:double computeWeight();
2.创建三个类Television、AirConditioner和WashMachine。这些类都实现了ComputeWeight接口,能够提供自重。其中TV的自重为16.6kg,AirConditioner的自重为40.0kg,WashMachine的自重为60.0kg。
3.创建一个Truck类,私有成员变量goods是一个ComputeWeight型的数组,包含了该货车上的所有家电,公有方法getTotalWeight()返回goods中所有货物的重量之和。
输入格式:
家电数量
家电种类编号
注意:各个家电的编号为:Television:1 , AirConditioner:2 , WashMachine:3
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如: public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n=input.nextInt(); int type; ComputeWeight [] goods=new ComputeWeight[n]; //n件货物 for(int i=0;i<goods.length ;i++){ type=input.nextInt(); //读入家电类型 if (type==1) goods[i]=new Television(); else if (type==2) goods[i]=new AirConditioner(); else if (type==3) goods[i]=new WashMachine(); } Truck truck =new Truck(goods); System.out.printf("货车装载的货物总量:%8.2f kg",truck.getTotalWeight()); } } /* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
10
1 2 3 3 2 1 3 3 3 2
输出样例:
在这里给出相应的输出。例如:
货车装载的货物总量: 453.20 kg
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
interface ComputeWeight {
double computeWeight();
}
class Television implements ComputeWeight {
@Override
public double computeWeight() {
return 16.6;
}
}
class AirConditioner implements ComputeWeight {
@Override
public double computeWeight() {
return 40.0;
}
}
class WashMachine implements ComputeWeight {
@Override
public double computeWeight() {
return 60.0;
}
}
class Truck {
ComputeWeight[] goods;
public Truck(ComputeWeight[] goods) {
this.goods = goods;
}
double getTotalWeight() {
double sum = 0;
for (ComputeWeight good : goods) {
sum += good.computeWeight();
}
return sum;
}
}