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

Implement a class Car with the following properties. A car has a certain fuel ef

ID: 3583217 • Letter: I

Question

Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a method drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and methods getGasLevel, to return the current fuel level, and addGas, to tank up. Sample usage:

Car myHybrid = new Car(50); // 50 miles per gallon myHbrid.addGas(20); // Tank 20 gallons myHybrid.drive(100); // Drive 100 miles System.out.println(myHybrid.getGasLevel()); //

Print fuel remaining Write a program to test your Car class.

Explanation / Answer

Here is code:

Car.java

public class Car
{
  
private double drive;
private double gas;
private double fuelEfficiency;
/**
@param an Efficiency the fuel efficiency of the car and gar and drive are assigned to zero
*/
public Car(double mpg)
{
fuelEfficiency = mpg;
gas = 0;
drive = 0;
}

// Adds gas to the tank.
public void addGas(double amount)
{
gas = gas + amount;
}

// Drives a certain amount, consuming gas

public void drive(double distance)
{
drive = drive + distance;
gas = gas - (distance / fuelEfficiency);
}

//Gets the amount of gas.

public double getGasLevel()
{
return gas;
}

}

Driving.java

public class Driving {
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
System.out.println(myHybrid.getGasLevel()); //
}
}

Output:

18.0

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