Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Created February 4, 2018 07:13
Show Gist options
  • Select an option

  • Save annibuliful/eeb683d591fd47faacaf8c7493e498c8 to your computer and use it in GitHub Desktop.

Select an option

Save annibuliful/eeb683d591fd47faacaf8c7493e498c8 to your computer and use it in GitHub Desktop.
public class Car {
public double efficiency;
public double carGas;
public double numberOfDistancePerLitter;
Car(double efficiency){
this.efficiency = efficiency;
}
public void addGas(double gas){
this.carGas = gas;
this.numberOfDistancePerLitter = this.carGas * this.efficiency;
}
public double getGasInTank(){
double carGas = this.numberOfDistancePerLitter / this.efficiency;
return carGas;
}
public void drive(double distance){
this.numberOfDistancePerLitter -= distance;
}
}
public class CarTester {
public static void main(String[] args){
Car myCar = new Car(20); // 20 kilometers per litter
myCar.addGas(40); // Add gasoline 40 litters
myCar.drive(100); // Drive 100 kilometers
double gasLeft = myCar.getGasInTank();
System.out.println(gasLeft);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment