编写程序,定义一个表示汽车信息的类 Car,要求如下:
(1)类 Car 的成员变量: cPlate 表示车牌;cBrand 表示品牌;cPrice 表示价格(单位:万元);cSaleDate 表示购买日期;cMasterName 表示购买者,cMasterPhone 表示购买者联系方式,cEvaluation 表示用户评价(“优”“良”“差”);(2)类 Car 带参数的构造方法:在构造方法中通
过形参完成对所有成员变量的初值赋值操作。(3)类 Car 的方法成员:用 get set 方法完成对所有成员变量的赋值、取值操作。根据类 Car 的定义,创建并用 List 存储 6 个该类的对象,并完成以下操作:
(1)输出每辆汽车的信息
(2)计算并输出车价分别在[0 10 万)、[10 万 30 万)、[30w 500w]价格区间的车辆数
(3)输出所有上海牌照的车牌
(4)统计并输出评价为“优”的汽车品牌
给我采纳,我马上写给你!:
代码都有详尽注释,希望对您有所帮助,如有帮助,望采纳
public class Car {
//cBrand 表示品牌;cPrice 表示价格(单位:万元);
// cSaleDate 表示购买日期;cMasterName 表示购买者,
// cMasterPhone 表示购买者联系方式,cEvaluation 表示用户评价(“优”“良”“差”);
private String cPlate;//车牌
private String cBrand;//品牌
private Integer cPrice;//价格(单位:万元)
private String cSaleDate;//购买日期;
private String cMasterName;//购买者
private String cMasterPhone;//购买者联系方式
private String cEvaluation;//表示用户评价(“优”“良”“差”)
//无参构造方法
public Car() {
}
//有参构造方法
public Car(String cPlate, String cBrand, Integer cPrice, String cSaleDate,
String cMasterName, String cMasterPhone, String cEvaluation) {
this.cPlate = cPlate;
this.cBrand = cBrand;
this.cPrice = cPrice;
this.cSaleDate = cSaleDate;
this.cMasterName = cMasterName;
this.cMasterPhone = cMasterPhone;
this.cEvaluation = cEvaluation;
}
//get set方法赋值
public String getcPlate() {
return cPlate;
}
public void setcPlate(String cPlate) {
this.cPlate = cPlate;
}
public String getcBrand() {
return cBrand;
}
public void setcBrand(String cBrand) {
this.cBrand = cBrand;
}
public Integer getcPrice() {
return cPrice;
}
public void setcPrice(Integer cPrice) {
this.cPrice = cPrice;
}
public String getcSaleDate() {
return cSaleDate;
}
public void setcSaleDate(String cSaleDate) {
this.cSaleDate = cSaleDate;
}
public String getcMasterName() {
return cMasterName;
}
public void setcMasterName(String cMasterName) {
this.cMasterName = cMasterName;
}
public String getcMasterPhone() {
return cMasterPhone;
}
public void setcMasterPhone(String cMasterPhone) {
this.cMasterPhone = cMasterPhone;
}
public String getcEvaluation() {
return cEvaluation;
}
public void setcEvaluation(String cEvaluation) {
this.cEvaluation = cEvaluation;
}
public void outPutCarInfo() {
System.out.println("车牌:" + this.cPlate + ", 品牌:" + this.cBrand +
", 价格:" + this.cPrice + ", 购买日期:" + this.cSaleDate
+ ", 购买者:" + this.cMasterName + ", 购买者联系方式:" + this.cMasterPhone + ", 用户评价:" + this.cEvaluation);
}
}
import java.util.ArrayList;
import java.util.List;
//测试类
public class Test {
public static void main(String[] args) {
//创建list存储car对象
List<Car> list = new ArrayList<>();
//创建6个car对象
Car car = null;
car = new Car("沪A00000", "奥迪", 20, "2023-01-05", "小白", "12345678901", "优");
list.add(car);
car = new Car("京A00000", "玛莎拉蒂", 200, "2023-01-05", "小花", "12345678901", "良");
list.add(car);
car = new Car("苏A00000", "奔驰", 40, "2023-01-05", "小红", "12345678901", "差");
list.add(car);
car = new Car("沪A00001", "大众", 8, "2023-01-05", "小绿", "12345678901", "优");
list.add(car);
car = new Car("鲁A00000", "奥迪", 20, "2023-01-05", "小黑", "12345678901", "良");
list.add(car);
car = new Car("沪A00002", "奥迪", 20, "2023-01-05", "小丑", "12345678901", "差");
list.add(car);
//输出每辆车信息
for (int i = 0; i < 6; i++) {
list.get(i).outPutCarInfo();
}
//计算并输出车价分别在[0 10 万)、[10 万 30 万)、[30w 500w]价格区间的车辆数
int a = 0;//[0 10 万)数量
int b = 0;//[10 万 30 万)
int c = 0;//[30w 500w]
for (int i = 0; i < 6; i++) {
Integer price = list.get(i).getcPrice();
if (price >= 0 && price < 10) {
a++;
}
if (price >= 10 && price < 30) {
b++;
}
if (price >= 30 && price <= 500) {
c++;
}
}
//输出车辆数
System.out.println("[0 10 万)车辆数为:" + a + "辆");
System.out.println("[10 万 30 万)车辆数为:" + b + "辆");
System.out.println("[30w 500w]车辆数为:" + c + "辆");
//输出所有上海牌照的车牌
String plateStr = "";
for (int i = 0; i < 6; i++) {
String plate = list.get(i).getcPlate();
if (plate.startsWith("沪")) {
plateStr = plateStr + plate + ",";
}
}
System.out.println("所有上海牌照的车牌:" + plateStr);
//统计并输出评价为“优”的汽车品牌
String brandStr = "";
for (int i = 0; i < 6; i++) {
String brand = list.get(i).getcBrand();
String evl = list.get(i).getcEvaluation();
if (evl.equals("优")) {
brandStr = brandStr + brand + ",";
}
}
System.out.println("评价为“优”的汽车品牌:" + brandStr);
}
}
测试结果如下: