java例题 汽油检测

package woxihuan;

public class Car {
            // 汽车的油量监控系统
            // 定义一个类Car
            // Car: 品牌, 平均油耗, 邮箱容量
            // 什么牌子的车行驶了多少公里, 消耗汽油多少L, 邮箱还剩多少L
        //get
        public Car(String _name, Double _sal, int _volume, Double _distance) {
            name = _name;
            sal = _sal;
            volume = _volume;
            distance = _distance;
            operation = sal * distance;
            operation1 = volume - operation;
        }
        //set
        public Car() {
            name = "奥迪";
            sal = 13.5;
            volume = 90;
            distance = 1.5;
            operation = sal * distance;
            operation1 = volume - operation;
            
        }
        
        private String name; //品牌
        private double sal;//油耗
        private int volume;//容量
        private double distance;//行驶了多少公里
        private double operation;//运算消耗汽油
        private double operation1;//运算油箱还剩多少
        
        public void result() {
            System.out.println(name + "行驶了" + distance + "km,消耗汽油:" + operation + "L,油箱还剩:" + operation1 + "L。" );
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
}
package woxihuan;

public class Che {

    public static void main(String[] args) {
        //get
        Car e = new Car("奥迪a3" ,8.2 , 55 ,2.6);
        e.result();
        
        //set
        Car e2 = new Car();
        e2.setName("奥迪r8");
        e2.result();
        
                

    }

}

 

posted @ 2018-05-04 10:24  峰栯  阅读(240)  评论(0编辑  收藏  举报