期末Java题库--编程题2

开发汽车租贸平台。设计一个汽车类Vehicle,包含名有属性车牌号num为String类型、车的品牌brand为String类型和日租金perRent为double类型,为私有属性分别创建get.set方法,其中setNum(String nym)方法设量同性车牌号num的长为6位,否则默认为"JB0000":setPerRent(double perRent)方法设置日租金perRent价格必须大于6否则默认为0。定义无参构造函数,默认num=“JB0000”、brand=“红旗”,perRent=50。定义一个含有三个参数的构造方法,参数为num、brand和perRent赋上传进来的值。定义一个含有一个参数的方法rent(int day),用于返回汽车租金,一个show()方法,用于输出车的关本信息和日租金。定义测试类PrOg1,测试类中使用两种构造函数创建Vehicle类的两个实例,一个为无参,一个为有参,其中有实例的参数车牌号为"A12345”、品牌为"东风”、日租全perRent为500,并测试Vehicle类的show()方法和rent(int day)方法,其中rent(int day)方法中的租赁天数为10天。
public class Proe1public static void main(string[] acgs)
往宠事场:
1、测试类类名必须为POg1,若使用其他命名方式则成通无效。
测试类主面数已经给出,请在牌译器中捞贝到谷题区域。
2、输出格式:每个变量问用英文过号隔开,题目中无行殊要求的情况下不允许参杂其他子符””+ XCX)3例2:System out.println(XXX +“,"
3、测试类中创建实例对象,每个对象按照无命、有参的顺序创建,
4、在弹出编译器的页面中program-end之问答怒,不允许新建页面

public class Prog1 {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle();
        v1.show();
        System.out.println(v1.rent(10));
        Truck v2 = new Truck("A12345", "东风", 500);
        v2.show();
        System.out.println(v2.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);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值