Java 类和对象12

构造一辆汽车,油箱容量100L,当前里程数0,当前油量0,可以根据道路状况确定油耗,根据行驶速度与行驶时间, 输出当前油量与总里程数。

  1 public class Car_1 {
  2 
  3     // 车牌
  4     private String CheP;
  5     // 邮箱容量 单位:L
  6     private double YouXRL = 100;
  7     // 总里程数 单位:km
  8     private double LiC = 0;
  9     // 油耗
 10     private double YouH = 10;
 11     // 当前油量 单位:L
 12     private double DangQYL = 0;
 13 
 14     public String getCheP() {
 15         return CheP;
 16     }
 17 
 18     public void setCheP(String cheP) {
 19         CheP = cheP;
 20     }
 21 
 22     public double getYouXRL() {
 23         return YouXRL;
 24     }
 25 
 26     public void setYouXRL(double youXRL) {
 27         YouXRL = youXRL;
 28     }
 29 
 30     public double getLiC() {
 31         return LiC;
 32     }
 33 
 34     public void setLiC(double liC) {
 35         LiC = liC;
 36     }
 37 
 38     public double getYouH() {
 39         return YouH;
 40     }
 41 
 42     public void setYouH(double youH) {
 43         YouH = youH;
 44     }
 45 
 46     public double getDangQYL() {
 47         return DangQYL;
 48     }
 49 
 50     public void setDangQYL(double dangQYL) {
 51         DangQYL = dangQYL;
 52     }
 53 
 54     public Car_1(String CheP) {
 55         this.CheP = CheP;
 56     }
 57 
 58     // 加油
 59     public void JiaYou(double youl) {
 60         if (youl < 0) {
 61             System.out.println("请不要偷油!");
 62         } else if (DangQYL + youl > YouXRL) {
 63             System.out.println("加油量=" + (YouXRL - DangQYL) + "L,油箱已满。");
 64             DangQYL = YouXRL;
 65         } else {
 66             System.out.println("加油量=" + youl+"L");
 67             DangQYL = DangQYL + youl;
 68         }
 69     }
 70 
 71     // 直接加满
 72     public double JiaYou() {
 73         double jyl = YouXRL - DangQYL;
 74         JiaYou(jyl);
 75         return jyl;
 76     }
 77 
 78     // 行驶
 79     public void XingShi(double ShiS, double time) {
 80         if (ShiS >= 0 && time >= 0) {
 81             if (DangQYL - YouH * ShiS * time / 100 >= 0) {
 82                 LiC += ShiS * time;
 83                 DangQYL -= ShiS * time * YouH / 100;
 84                 System.out.println("当前油量=" + DangQYL+"L。");
 85                 System.out.println("行驶里程总数=" + LiC+"km。");
 86             } else {
 87                 double sj = DangQYL * 100 / YouH / ShiS;
 88                 System.out.println("油量不足,按照" + ShiS + "km/h的时速,预计可以行驶" + sj + "小时。");
 89             }
 90         } else {
 91             System.out.println("转速表或时间表出错,请检查!");
 92         }
 93     }
 94 
 95     public static void main(String[] args) {
 96         //构造第一辆车
 97         Car_1 car1 = new Car_1("鲁C1234");
 98 
 99         car1.JiaYou(10);
100         car1.JiaYou();
101 
102         car1.setYouH(10);// 根据不同的道路情况设置油耗
103         car1.XingShi(180, 1);// 行驶速度与行驶时间
104         car1.XingShi(200, 1);
105         car1.XingShi(150, 5);
106         car1.JiaYou(30);
107         car1.XingShi(150, 5);
108         car1.XingShi(-150, 5);
109         car1.XingShi(150, -3);
110 
111     }

运行:

posted @ 2016-05-20 16:41  唐枫  阅读(161)  评论(0编辑  收藏  举报