期末java题库--编程题1

设计一个汽车类Vehicle,包含私有属性车牌号num为String类型、车的品牌brand为String类型、日租金perRent为double类型,为私有属性分别创建get、set方法,其中setNum(String num)方法设置属性车牌号num的长度为6位,否则默认为"JB0000",setPerRent(double perRent)方法日租金perRent价格必须大于0,否则默认为0.定义无参构造函数,默认num=“JB0000”、brand=“红旗”、perRent=50,定义一个含有参数的构造函数,为num、brand和perRent赋上传进来的值,定义一个含有一个参数的方法rent(int day),用于返回汽车租金,一个show0方法,用于输出车的基本信息和日租金。由Vehicle类派生子类货车类Truck,新增私有属性卡车载重量load为double类型并为私有属性分别创建get、set方法,提供货车的构造函数两个,一个无参继承父类属性,其中默认载重是load=10,一个含有4个参数,重写rent(int day)方法当租借的货车载重量超过50吨,出租天数大于等于三天租金打九折;重写show0方法输出卡车的所有信息和载重量;定义测试类Prog1,测试类中使用构造函数创建Truck类的两个实例,一个为无参,一个为有参,其中有参的构造函数参数num为"AB5678"、brand为“解放”、perRent为200、load为1000。测试Truck类的show0方法和rent(int day)方法,其中rent(int day)方法中的租赁天数为10天。(输出格式:每个变是间用英文逗号隔开public class Prog1 {
public static void main(Stringà args) {
注意事项
1、测试类类名必须为Prog1,若使用其他命名方式则成绩无效
测试类主函数已经给出,请在编译器中拷贝到答题区域。
2、输出格式:每个变是间用英文逗号隔开,题目中无特殊要求的情况下不允许参杂其他字符。
例如:System.out.printinÃxx+“” + xxX);
3、测试类中创建实例对象,每个对象按照无参、有参的顺序创建。
4、在弹出编译器的页面中program-end之间答题,不允许新建页面,

public class Prog1 {
    public static void main(String[] args) {
        Truck t1 = new Truck();
        t1.show();
        System.out.println(t1.rent(10));
        Truck t2 = new Truck("AB5678", "解放", 200, 1000);
        t2.show();
        System.out.println(t2.rent(10));
    }
}

class Vehicle {
    private String num;
    private String brand;
    private double perRent;

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        if (num.length() == 6) {
            this.num = num;
        } else {
            this.num = "JB0000";
        }
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPerRent() {
        return perRent;
    }

    public void setPerRent(double perRent) {
        if (perRent > 0) {
            this.perRent = perRent;
        } else {
            this.perRent = 0;
        }
    }

    public Vehicle() {
        this.num = "JB0000";
        this.brand = "红旗";
        this.perRent = 50;
    }

    public Vehicle(String num, String brand, double perRent) {
        this.num = num;
        this.brand = brand;
        this.perRent = perRent;
    }

    public double rent(int day) {
        return this.perRent * day;
    }

    public void show() {
        System.out.println(this.num + "," + this.brand + "," + this.perRent);
    }
}

class Truck extends Vehicle {
    private double load;

    public double getLoad() {
        return load;
    }

    public void setLoad(double load) {
        this.load = load;
    }

    public Truck() {
        super();
        this.load = 10;
    }

    public Truck(String num, String brand, double perRent, double load) {
        super(num, brand, perRent);
        this.load = load;
    }

    @Override
    public double rent(int day) {
        if (day >= 3 && this.load > 50) {
            return getPerRent() * day * 0.9;
        } else {
            return getPerRent() * day;
        }
    }

    @Override
    public void show() {
        System.out.println(getNum() + "," + getBrand() + "," + getPerRent() + "," + load);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值