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

This needs to be done in Java please. Implement a class Car with the following p

ID: 3678887 • Letter: T

Question

This needs to be done in Java please. Implement a class Car with the following properties:

A car has a fuel efficiency (measured in miles per gallon) and an amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0 (zero). Supply a method called drive that simulates driving the car for a certain distance, reducing the amount of gasoline in the fuel tank. Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank, and addGas, to add gasoline to the fuel tank.

Sample usage of the Car class:

Car myHybrid = new Car(50); //50 miles per gallon

myHybrid.addGas(20); //add 20 gallons to the car's tank

myHybrid.drive(100); //drive the car 100 miles //

Get the gas remaining in the fuel tank

double gasLeft = myHybrid.getGasInTank();

In your CarTester application, show the amount of gas left in the fuel tank – from the above code – and the Expected amount. Repeat this exercise but with a Buick that gets 23 miles per gallon.

The expected results are: myHybrid left with 18.0 gallons of gas in the tank. the buick is left with 15.7 gallons of gas in the tank. Thanks!

Explanation / Answer

class CarTest
{
public static void main(String[] args)
{
      //instantiate Hybrid car
    Car Hybrid = new Car(50);
    Hybrid.addGas(20);
    Hybrid.drive(100);
    System.out.println(" My hybrid is left with "+Hybrid.getGas()+" gas of gallon");
  
    // instantiate Buick car
    Car Buick = new Car(23);
    Buick .addGas(20);
    Buick .drive(100);
    System.out.println(" My Buick is left with "+Buick.getGas()+" gas of gallon");
}
}

class Car
{

public Car(double mpg)
{
milesPerGallon = mpg;
gas = 0;
drive = 0;
}


public void addGas(double amount)
{
gas = gas + amount;
}

public double getGas()
{
    return gas;
}

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

private double drive;
private double gas;
private double milesPerGallon;
}

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