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

USING JAVA Implement a class Car, which contains the fields: 1.make, e.g. Ford,

ID: 3817988 • Letter: U

Question

USING JAVA

Implement a class Car, which contains the fields:

1.make, e.g. Ford, Subaru, Toyota ...

2.model, e.g., Escape, Outback, Camry ...

3.year

4.MPG miles per gallon

5.milesDriven, the total number of miles ever driven in this car.

6.fuelCapacity in gallons, i.e., the size in gallons of the fuel tank.

7.fuelRemaining, which represents the amount of fuel remaining in the gas tank.

Implement at least the following methods within the Car class:

>a constructor, which initializes each of the fields

>fillTank(double g), which adds up to g gallons of gas to the fuel tank, but not more than the car's fuel capacity.

>drive(double m), which simulates driving m miles in the car, adding to the total number of miles driven, and reducing the amount of gas in the car according to this car's average MPG.

>toString( ), which returns a String representation of the car.

>getFuelRemaining( ), which returns the amount of fuel left in the tank

Explanation / Answer

class Car {
     String make,model;
      int year;
     double MPG,milesDriven,fuelCapacity ,fuelRemaining;
        //costructor
        public Car ( String _make,String _model,int _year,double _MPG, double _milesDriven,double _fuelCap,double _fuelRemain)
        {
            make=_make;
           model=_model;
           year=_year;
           MPG=_MPG;
           milesDriven=_milesDriven;
           fuelCapacity=_fuelCap;
           fuelRemaining=_fuelRemain;
        }
       public String getMake(){return make;}
       public String getModel(){return model;}
       public int getYear(){return year;}
       public double getmilesDriven(){return milesDriven;}
       public double getFuelCap(){return fuelCapacity;}
       public void fillTank(double g){
           if(fuelCapacity<=fuelRemaining+g)
          fuelRemaining=fuelRemaining+g;
      else
          System.out.println("Exceeding the fuel capacity ");
         
       }
       public void drive(double m){
       milesDriven=milesDriven+m;
       fuelRemaining = fuelRemaining - (milesDriven / MPG);
       }
       public double getMPG(){return MPG;}
       public double getFuelRemaining( ){
           return fuelRemaining;
       }
       public String toString(){
           return "The "+getMake()+" "+getModel() +" " + getYear()+" car has fuel capacity of "+getFuelCap()+" gallons.The MPG is "+getMPG()+" miles per gallon. The miles driven is "+getmilesDriven()+" and amount of fuel remaining in car is "+getFuelRemaining()+" gallon.";
       }
       public static void main(String[] args) {
           Car carObj=new Car("Ford","Escape",2014,100,0, 10.5,10.5);
           System.out.println(carObj.toString());
           carObj.drive(10);
           System.out.println("Now,the miles driven is "+carObj.getmilesDriven()+" and amount of fuel remaining in car is "+carObj.getFuelRemaining()+" gallons.");
       }
}