Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A car has a certain fuel efficiency (measured in miles/gallonor liters/km - pick

ID: 3613383 • Letter: A

Question

A car has a certain fuel efficiency (measured in miles/gallonor liters/km - pick one) and a certain amount of fuel in the gastank. The efficiency is specified in the constructor, and theinitial fuel level is 0.
Supply a method drive thatsimulates driving the car for a certain distance, reducing theamount of gasoline in the fuel tank. Also supply methodsgetGasInTank , returning thecurrent amount of gasoline in the fuel tank, and addGas , to add gasoline to the fuel tank.Sample usage: A car has a certain fuel efficiency (measured in miles/gallonor liters/km - pick one) and a certain amount of fuel in the gastank. The efficiency is specified in the constructor, and theinitial fuel level is 0.
Supply a method drive thatsimulates driving the car for a certain distance, reducing theamount of gasoline in the fuel tank. Also supply methodsgetGasInTank , returning thecurrent amount of gasoline in the fuel tank, and addGas , to add gasoline to the fuel tank.Sample usage:

Explanation / Answer

please rate - thanks TEST PROGRAM class CarTester { public static void main(String[] args) {Car myHybrid = new Car(50); // 50 miles per gallon myHybrid.addGas(20); // Tank 20 gallons myHybrid.drive(100); // Drive 100 miles double gasLeft = myHybrid.getGasInTank(); // Get gas remaining intank System.out.println("Gas left in tank is: "+ gasLeft +"gallons"); } } CLASS class Car { private double mpg; private double gallons=0; public Car(double gal) { mpg=gal; } public void addGas(double amount) { gallons+= amount; } public void drive(double amount) {double used=amount/mpg; gallons-=used; } public double getGasInTank() { return gallons; } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote