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

VERY BEGINNER JAva program please. Please add comments explaining every step. Im

ID: 3869055 • Letter: V

Question

VERY BEGINNER JAva program please. Please add comments explaining every step.

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.

Explanation / Answer

public class Car {

private int milesPerGallon;

private int fuel;

/**

* @param milesPerGallon

* @param fuel

*/

public Car(int milesPerGallon) {

this.milesPerGallon = milesPerGallon;

this.fuel = 0;

}

/**

* @param fuel

*/

public void addGas(int fuel) {

this.fuel += fuel;

}

/**

* @param miles

*/

public void drive(int miles) {

this.fuel -= (miles / milesPerGallon);

}

/**

* @return

*/

public int getGasLevel() {

return fuel;

}

}

public class CarTest {

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.

}

}

OUTPUT:

18