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 cartain 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
myHybrid.addGas(20); // Tank 20 gallons
myHybrid.drive(100); // Drive 100 miles
System.out.println(myHybrid.getGasLevel()); // Print fuel remaining.

Respuesta :

Answer:

See explaination

Explanation:

class Car

{

private double drdist;

private double fuel;

private double mpg;

public Car(double m)

{

mpg = m;

fuel = 0;

}

public void addGas(double add)

{

fuel = fuel + add;

}

public void drive(double d)

{

drdist = drdist + d;

mpg = fuel * mpg / d;

}

public double getGasLevel()

{

return fuel;

}

}

public class Carfuel{

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()); // Print fuel remaining.

}

}

Classes are simply templates, with which methods are created.

The class Car and other methods written in Java, where comments are used to explain each line is as follows:

//This defines the Car class

class Car{

   //The next three lines defines the variables

   private double drdist;

   private double fuel;

   private double mpg;

   //This creates the car method

   public Car(double m){

       //This initializes the mileage

       mpg = m;

       //This initializes the amount of fuel to 0

       fuel = 0;

   }

   //This creates the addGas method

   public void addGas(double add){

       //This increases the amount of fuel

       fuel += add;

   }

   //This creates the drive method

   public void drive(double d){

       //This calculates the distance traveled

       drdist += d;

       //This calculates the mileage

       mpg = fuel * mpg / d

   }

   //This creates the getGasLevel method

   public double getGasLevel(){

       //This returns the amount of fuel

       return fuel;

   }

}

//This main class begins here

public class Carfuel{

   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()); // Print fuel remaining

   }

}

Read more about Java class at:

https://brainly.com/question/10124207