(1) 汽车接口(Car):有两个方法, getName()、getPrice()(接口源文件可以自己写,也可以直接从题目中下载) (2) BMW类,实现汽车接口 (3) 奇瑞QQ类,实现汽车接口 (4) 桑塔那类,实现汽车接口 (4) 汽车出售店(CarShop)类,有成员变量:count(售出数量) 和total(总价),有方法: void sellCar(Car car); (调用方法获取售出车的车型、售价;获取售出数量及总价) int getCount(); int getTotal(); (5) 编写测试类:卖出一辆BMW、一辆奇瑞QQ、一辆桑塔那,输出结果. 【输入形式】 无 【输出形式】 卖出的第一辆车车型、单价 汽车店的总收入、卖出车数量 卖出的第二辆车车型、单价 汽车店的总收入、卖出车数量 卖出的第三辆车车型、单价 汽车店的总收入、卖出车数量 表4.1实验题目1测试用例 序号 样例输入 样例输出 1 无 car model:BMW price:300000 total income:300000 count of sold:1 car model:CherryQQ price:20000 total income:320000 count of sold:2 car model:Santana price:280000 total income:600000 count of sold:3
时间: 2023-12-03 14:45:27 浏览: 84
以下是Java代码实现:
Car接口:
```
public interface Car {
String getName();
int getPrice();
}
```
BMW类:
```
public class BMW implements Car {
private String name = "BMW";
private int price = 300000;
@Override
public String getName() {
return name;
}
@Override
public int getPrice() {
return price;
}
}
```
奇瑞QQ类:
```
public class CherryQQ implements Car {
private String name = "CherryQQ";
private int price = 20000;
@Override
public String getName() {
return name;
}
@Override
public int getPrice() {
return price;
}
}
```
桑塔那类:
```
public class Santana implements Car {
private String name = "Santana";
private int price = 280000;
@Override
public String getName() {
return name;
}
@Override
public int getPrice() {
return price;
}
}
```
汽车出售店类:
```
public class CarShop {
private int count = 0;
private int total = 0;
public void sellCar(Car car) {
System.out.println("car model:" + car.getName() + " price:" + car.getPrice());
count++;
total += car.getPrice();
System.out.println("total income:" + total + " count of sold:" + count);
}
public int getCount() {
return count;
}
public int getTotal() {
return total;
}
}
```
测试类:
```
public class Test {
public static void main(String[] args) {
CarShop shop = new CarShop();
BMW bmw = new BMW();
CherryQQ qq = new CherryQQ();
Santana santana = new Santana();
shop.sellCar(bmw);
shop.sellCar(qq);
shop.sellCar(santana);
}
}
```
输出结果:
```
car model:BMW price:300000
total income:300000 count of sold:1
car model:CherryQQ price:20000
total income:320000 count of sold:2
car model:Santana price:280000
total income:600000 count of sold:3
```