java汽车工厂类方法_Java汽车构造函数

好吧,所以我有这个课程的测试人员,它一直在说以下消息:

“现在测试您的驱动方法:

***行驶700英里,行驶400英里,然后用完汽油的汽车现在不应具有1700.0英里的行驶里程。”

但是,当我删除里程或gasNeeded或gasInTank时,它会说正在运行的那个正在运行,但不能同时运行!

任何帮助表示赞赏!

public class Car

{

private double mpg;

private double mileage;

private double tankCapacity;

private double gasInTank;

/**

Constructs a car.

@param miles miles per gallon that a car gets

@param milesOnCar total mileage on car

@param gasTankHolds amount of gas the tank holds

@param gasTankHas gas in tank now

*/

public Car(double miles, double milesOnCar,

double gasTankHolds, double gasTankHas)

{

mpg = miles;

mileage = milesOnCar;

tankCapacity = gasTankHolds;

gasInTank = gasTankHas;

}

/**

Returns the current mileage on the car.

@return mileage on car

*/

public double getMileage()

{

return mileage;

}

/**

Returns the amount of gas in the gas tank.

@return amount of gas in tank

*/

public double getGasInTank()

{

return gasInTank;

}

/**

Returns the gas needed to drive miles (based on mpg)

@param numMiles the number of miles to be driven

@return gas needed to drive numMiles miles

*/

public double gasNeeded(double numMiles)

{

double gasNeeded = numMiles/mpg ;

if(numMiles == 0)

{

return 0 ;

}

else if(mpg == 0)

{

return 0 ;

}

else

return gasNeeded ;

}

/**

Returns true if the car has enough gas in the tank to

drive numMiles, otherwise returns false.

@param numMiles miles to be driven

@return true if gas in tank is enough to drive numMiles

miles; false otherwise.

*/

public boolean enoughGas(double numMiles)

{

double gasNeeded = numMiles/mpg ;

if(gasInTank == gasNeeded)

{

return true ;

}

else

{

return false;

}

}

/**

If tank is less than half full, fills tank and updates

gasInTank. Otherwise does nothing.

*/

public void getGas()

{ double gas2 = 1.0/2.0 * tankCapacity ;

if(gasInTank <= gas2)

{

gasInTank = tankCapacity ;

}

}

/**

Updates mileage and gasInTank to reflect numMiles

being driven.

@param numMiles number of miles driven

*/ /**

Updates mileage and gasInTank to reflect numMiles

being driven.

@param numMiles number of miles driven

*/

public void drive(double numMiles)

{

double gasNeeded = numMiles/mpg ;

if(numMiles != 0 )

{

mileage = mileage + numMiles ;

}

if(gasInTank> gasNeeded)

{

gasInTank = gasInTank - gasNeeded ;

}

if(gasInTank < gasNeeded){

gasInTank = 0 ;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值