public class Odometer {
x09x09//journey初始位置,surplusJourney剩余燃油可行驶里程
x09x09private double journey,surplusJourney;
x09x09//剩余燃油
x09x09private double fuel;
x09x09//燃油效率
x09x09private double efficiency;
x09x09//构造方法,在创建Odometer对象时设置初值
x09x09public Odometer(){
x09x09x09journey = 0;//初始位置为0
x09x09x09fuel = 60;//初始燃料为60
x09x09x09efficiency = 50;//初始效率为50%
x09x09}
x09x09
x09x09//设置初始位置
x09x09public void setdistance(){
x09x09x09journey = 0;
x09x09}
x09x09//设置燃油效率
x09x09public void setEfficiency(double e){
x09x09x09efficiency = e;
x09x09}
x09x09//返回剩余燃油
x09x09public double getFuel(double f){
x09x09x09journey += f;
x09x09x09fuel -= f / efficiency;
x09x09x09System.out.println("剩余燃料:"+fuel);
x09x09x09return fuel;
x09x09}
x09x09//返回剩余燃油可行驶里程
x09x09public double getSurplusJourney(){
x09x09x09surplusJourney = fuel * efficiency;
x09x09x09return surplusJourney;
x09x09}
x09x09//输出剩余燃油可走的路程
x09x09public String toString(){
x09x09x09return "剩余燃油可走的路程:" + getSurplusJourney();
x09x09}
}
public class TestOdometer {
x09public static void main(String[] args){
x09x09//输入的旅行距离
x09x09double journey;
x09x09//输入的燃油效率
x09x09double efficiency;
x09x09//创建Odometer对象,调用Odometer的构造方法设置初始值
x09x09Odometer odometer = new Odometer();
x09x09//从控制台输入旅行距离
x09x09Scanner scan = new Scanner(System.in);
x09x09System.out.println("请输入旅行距离:");
x09x09journey = scan.nextDouble();
x09x09//返回剩余燃料
x09x09odometer.getFuel(journey);
x09x09//输出剩余燃料按照50%的 效率可行驶的路程
x09x09System.out.println(odometer);
x09x09//输入新的燃油效率
x09x09System.out.println("请输入燃油效率:");
x09x09efficiency = scan.nextDouble();
x09x09odometer.setEfficiency(efficiency);
x09x09//输出剩余燃料按照新的效率可行驶的路程
x09x09System.out.println(odometer);
x09}
}
这样看得懂不?