Please I need your help with this project on Java code Implement a class Car. A
ID: 3648070 • Letter: P
Question
Please I need your help with this project on Java codeImplement a class Car. A Car object should have three instance variables, one for fuel efficiency (representing miles per gallon), one for fuel level (representing gallons), and a variable that acts as an odometer (representing miles).
The fuel efficiency of a car should be specified as a parameter in the Car constructor and the constructor should set the fuel level and the odometer to zero. There should be getFuelEfficiency(), getOdometer(), and getFuelLevel() methods.
There should also be a method addFuel(double gallons) which adds a specified amount to the fuel level and returns the new fuel level, and there should be a method drive(double miles) which simulates driving the car a specified distance.
The drive() method should adjust the fuel level by the amount of fuel used, adjust the odometer by the amount of miles driven, and it should return the number of miles driven, which may be less than the number of miles specified if there is not enough fuel. (Notice that there are no setFuelEfficiency(), setOdometer(), and setFuelLevel() methods. The fuel efficiency field is immutable; once it is set by the constructor, it cannot be changed. The odometer should only be changeable by driving the car, as in a real car. The fuel level should only be changed by driving the car or by adding fuel.)
Explanation / Answer
//Revised Car Class public class Car { private double fuelEfficiency; //Represents miles per gallon private double fuelLevel; //Represents gallons private double odometer; //Represents miles public Car(double fuelEfficiency) { this.fuelEfficiency = fuelEfficiency; this.fuelLevel = 0.0; this.odometer = 0.0; } public double getFuelEfficiency() { return this.fuelEfficiency; } public double getFuelLevel() { return this.fuelLevel; } public double getOdometer() { return this.odometer; } public double drive(double miles) { double distance = (this.fuelLevel * this.fuelEfficiency); //gallons * MPG = total distance if(distance > miles) { distance = miles; } this.fuelLevel = (fuelLevel - (distance/fuelEfficiency)); this.odometer = odometer + distance; return distance; } public double addFuel(double gallons) { this.fuelLevel = this.fuelLevel + gallons; return this.fuelLevel; } }