圥忈.425 2022-05-30 19:01 采纳率: 85.7%
浏览 84
已结题

java实验-设计一个用于交通工具管理的程序

设计一个用于交通工具管理的程序,具体实现以下要求:
(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)从键盘输入车辆的信息(要求包括小汽车、卡车和客货两用车),保存在某类集合(具体集合类自己选择)中,应用多态编程输出所有车的信息,计算所有车的总价格并输出。

  • 写回答

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);
        }    
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 创建了问题 5月30日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!