Construct a car, fuel tank capacity 100L, current mileage 0, current oil volume 0, can determine the fuel consumption according to the road condition, according to the driving speed and travel time, output current oil volume and total mileage.
1 Public classcar_1 {2 3 //License plate4 PrivateString CheP;5 //Mailbox Capacity Unit: L6 Private DoubleYOUXRL = 100;7 //Total mileage units: km8 Private DoubleLiC = 0;9 //Fuel ConsumptionTen Private DoubleYouh = 10; One //Current Oil Quantity unit: L A Private DoubleDangqyl = 0; - - PublicString Getchep () { the returnCheP; - } - - Public voidSetchep (String cheP) { +CheP =CheP; - } + A Public DoubleGetyouxrl () { at returnYouxrl; - } - - Public voidSETYOUXRL (DoubleYOUXRL) { -YOUXRL =Youxrl; - } in - Public Doublegetlic () { to returnLiC; + } - the Public voidSetlic (DoubleLiC) { *LiC =LiC; $ }Panax Notoginseng - Public DoubleGetyouh () { the returnYouh; + } A the Public voidSetyouh (DoubleYouh) { +Youh =Youh; - } $ $ Public DoubleGetdangqyl () { - returnDangqyl; - } the - Public voidSetdangqyl (Doubledangqyl) {WuyiDangqyl =Dangqyl; the } - Wu Publiccar_1 (String CheP) { - This. CheP =CheP; About } $ - //come on - Public voidJiayou (DoubleYoul) { - if(Youl < 0) { ASYSTEM.OUT.PRINTLN ("Please don't steal the oil!" "); +}Else if(Dangqyl + Youl >YOUXRL) { theSYSTEM.OUT.PRINTLN ("Refueling amount =" + (youxrl-dangqyl) + "L, the tank is full. "); -Dangqyl =Youxrl; $}Else { theSYSTEM.OUT.PRINTLN ("Refueling amount =" + youl+ "L"); theDangqyl = Dangqyl +Youl; the } the } - in //Direct Fill the Public Doublejiayou () { the DoubleJyl = YOUXRL-Dangqyl; About jiayou (jyl); the returnJyl; the } the + //Driving - Public voidXingshi (DoubleShis,DoubleTime ) { the if(shis >= 0 && time >= 0) {Bayi if(Dangqyl-youh * shis * time/100 >= 0) { theLiC + = Shis *Time ; theDangqyl-= shis * Time * youh/100; -SYSTEM.OUT.PRINTLN ("current Oil volume =" + dangqyl+ "L. "); -SYSTEM.OUT.PRINTLN ("total mileage =" + lic+ "km). "); the}Else { the DoubleSJ = Dangqyl * 100/youh/shis; theSYSTEM.OUT.PRINTLN ("Low oil volume, according to" + Shis + "km/h speed, is expected to travel" + SJ + "hours. "); the } -}Else { theSYSTEM.OUT.PRINTLN ("Error in tachometer or schedule, please check!") "); the } the }94 the Public Static voidMain (string[] args) { the //construct the first car thecar_1 car1 =NewCar_1 ("Lu C1234");98 AboutCar1. Jiayou (10); - car1. Jiayou ();101 102Car1.setyouh (10);//set fuel consumption according to different road conditions103Car1. Xingshi (180, 1);//travel speed and travel time104Car1. Xingshi (200, 1); theCar1. Xingshi (150, 5);106Car1. Jiayou (30);107Car1. Xingshi (150, 5);108Car1. Xingshi (-150, 5);109Car1. Xingshi (150,-3); the 111}
Run:
Java Classes and Objects 12