Problem A: 实验四:计算重量

该程序定义了一个ComputerWeight接口,三个类Television、Computer、WashMachine分别实现了这个接口来计算自身重量。Car类持有一个ComputerWeight对象数组,通过遍历数组计算总重量。最终程序输出两辆Car对象(大型卡车和皮卡)装载的物品总重量。
摘要由CSDN通过智能技术生成

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());
    }
}

题目描述 定义一个Point,表示平面上的点,具有x和y两个坐标成员变量,以及设置和获取坐标的方法。还要实现计算两点之间距离的方法。 输入描述 无输入。 输出描述 无输出。 样例 无样例。 提示 要求定义Point,具有如下成员: 成员变量: double x:表示点的横坐标。 double y:表示点的纵坐标。 成员函数: Point():构造函数,将x和y初始值为0.0。 Point(double x, double y):构造函数,将x和y初始值为参数值。 void setX(double x):设置点的横坐标。 double getX():获取点的横坐标。 void setY(double y):设置点的纵坐标。 double getY():获取点的纵坐标。 double distance(Point another):计算当前点与另外一个点之间的距离,返回距离值。 注意:在实现中,要包含头文件cmath,使用其中的sqrt函数求平方根。 C++ 代码 ```cpp #include <iostream> #include <cmath> using namespace std; class Point { private: double x, y; public: Point() : x(0.0), y(0.0) {} Point(double x, double y) : x(x), y(y) {} void setX(double x) { this->x = x; } double getX() { return x; } void setY(double y) { this->y = y; } double getY() { return y; } double distance(Point another) { double dx = x - another.x; double dy = y - another.y; return sqrt(dx * dx + dy * dy); } }; int main() { Point p1, p2(3.0, 4.0); p1.setX(1.0); p1.setY(2.0); cout << "p1: (" << p1.getX() << ", " << p1.getY() << ")" << endl; cout << "p2: (" << p2.getX() << ", " << p2.getY() << ")" << endl; cout << "distance between p1 and p2: " << p1.distance(p2) << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小木苓

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

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

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

打赏作者

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

抵扣说明:

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

余额充值