Problem A: 实验三:编写汽车类

Problem Description

1.实验目的
    (1) 熟悉类的创建方法
    (2) 掌握对象的声明与创建
    (3) 能利用面向对象的思想解决一般问题
 
2.实验内容 
   编写一个java程序,设计一个汽车类Vehicle,包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类,包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

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

  public class Main{
    public static void main(String[] args){
        Vehicle v=new Vehicle(8,10.00);
        smallCar c=new smallCar(6);
        Truck t=new Truck(10);
        v.disMessage();
        c.disM();
        t.disM2();
        t.disM3();
   }
}
    // 你的代码

Sample Output

The number of wheels in this car is 8, weight is 10.0
This car can carry 6 persons
The load of this truck is 10
The number of wheels in this truck is 8, weight is 10.0, can carry 6 persons, load of this truck is 10

 我的代码:

public class Main{
    public static void main(String[] args){
        Vehicle v=new Vehicle(8,10.00);
        smallCar c = new smallCar(6);
        Truck t=new Truck(10);
        v.disMessage();
        c.disM();
        t.disM2();
        t.disM3();
    }
}
// 你的代码
class  Vehicle{
    //车轮的个数
    static int wheels;
    //车重
    static double weight;
    public Vehicle(){}
    public Vehicle(int i, double v) {
        wheels = i;
        weight = v;
    }


    public void disMessage() {
        System.out.println("The number of wheels in this car is "+ wheels + ", weight is " + weight);
    }
}

class  smallCar extends Vehicle{

    //载人数
    static int loader;

    public smallCar() {
    }

    public void disM(){
        System.out.println("This car can carry "+ loader +" persons");
    }

    public smallCar(int i){
        super();
        loader = i;
    }

    public int getWheels(){
        return wheels;
    }


    public double getWeight(){
        return  weight;
    }
}


class Truck extends smallCar{

    //载重量
    static int payload;
    public Truck(int i){
        super();
        payload = i;
    }

    public void disM2(){
        System.out.println("The load of this truck is " + payload);
    }


    public void disM3(){
        System.out.println("The number of wheels in this truck is " + wheels + ", weight is " + weight+
                ", can " +
                "carry " + loader + " persons," +
                " " +
                "load of this truck is " + payload);
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
题目描述 定义一个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、付费专栏及课程。

余额充值