设计一个用于交通工具管理的程序,具体实现以下要求:
(1)定义一个名为Vehicles(交通工具)的抽象类,类中包含的实例字段有:brand(品牌名称)、color(颜色)、price(价格),包含方法showInfo(在控制台显示品牌名称、颜色与价格),并编写构造方法初始化其实例字段。
(2)定义Car(小汽车)类继承于Vehicles类,增加int型实例字段seats(座位数),重写showInfo方法(在控制台显示小汽车信息),并编写构造方法。
(3)编写Truck(卡车)类继承于Vehicles类,增加float型实例字段load(载重),重写showInfo方法(在控制台显示卡车的信息),并编写构造方法。
(4)编写Van(客货两用车)类继承于Vehicles类,增加int型实例字段seats(座位数量)和float型实例字段load(载重),重写showInfo方法(在控制台显示客货两用车的信息),并编写构造方法。
(5)从键盘输入车辆的信息(要求包括小汽车、卡车和客货两用车),保存在某类集合(具体集合类自己选择)中,应用多态编程输出所有车的信息,计算所有车的总价格并输出。
java实验-设计一个用于交通工具管理的程序
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- a5156520 2022-06-02 16:40关注
一个实现,showInfo的输出信息可按自己需求调整,供参考:
import java.util.ArrayList; import java.util.Scanner; public class CarTest { public static void main(String[] args) { // TODO Auto-generated method stub //把交通工具类作为泛型,方便添加其子类,及下面的遍历 ArrayList<Vehicles> cars = new ArrayList<Vehicles>(); String brand = null; String color = null; double price = 0; int seats = 0; float load = 0; //从输入获取小车信息 System.out.println("请输入小汽车的信息:"); Scanner sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("座位数:"); seats = sc.nextInt(); System.out.print("价格:"); price = sc.nextDouble(); Car car = new Car(brand,color,price,seats); cars.add(car); //从输入获取卡车信息 System.out.println("请输入卡车的信息:"); sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("载重:"); load = sc.nextFloat(); System.out.print("价格:"); price = sc.nextDouble(); Truck truck = new Truck(brand,color,price,load); cars.add(truck); //从输入获取客货两用车信息 System.out.println("请输入客货两用车的信息:"); sc = new Scanner(System.in); System.out.print("品牌名称:"); brand = sc.next(); System.out.print("颜色:"); color = sc.next(); System.out.print("座位数:"); seats = sc.nextInt(); System.out.print("价格:"); price = sc.nextDouble(); Van van = new Van(brand,color,price,seats); cars.add(van); //用for-each语法遍历ArrayList集合 for(Vehicles v:cars) { v.showInfo(); } } } //交通工具抽象类 abstract class Vehicles{ public String brand; public String color; public double price; public abstract void showInfo(); public Vehicles(String brand,String color, double price) { this.brand = brand; this.color = color; this.price = price; } } //小汽车类 class Car extends Vehicles{ public int seats; public Car(String brand, String color, double price,int seats) { super(brand, color, price); this.seats = seats; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Car information :\n" +" brand: "+this.brand +" color: "+this.color +" seats: "+this.seats +" price: "+this.price); } } //卡车类 class Truck extends Vehicles{ public float load; public Truck(String brand, String color, double price,float load) { super(brand, color, price); this.load = load; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Truck information :\n" +" brand: "+this.brand +" color: "+this.color +" load: "+this.load +" price: "+this.price); } } //客货两用车类 class Van extends Vehicles{ public int seats; public Van(String brand, String color, double price,int seats) { super(brand, color, price); this.seats = seats; // TODO Auto-generated constructor stub } @Override public void showInfo() { // TODO Auto-generated method stub System.out.println("Van information :\n" +" brand: "+this.brand +" color: "+this.color +" seats: "+this.seats +" price: "+this.price); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥15 精通抖音评论点赞接口的来!需要开发
- ¥15 VB.NET2022如何生成发布成exe文件
- ¥30 matlab appdesigner私有函数嵌套整合
- ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
- ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
- ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
- ¥15 关于freesurfer使用freeview可视化的问题
- ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
- ¥15 求SC-LIWC词典!
- ¥20 有关esp8266连接阿里云