设计一个汽车类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);
}
}