fork download
  1. /*
  2. @Author:
  3. @Version : 1.0.0
  4. */
  5.  
  6. //car class implementation
  7. class Car{
  8. //private attributes of the class
  9. private int fuel, efficiency;
  10.  
  11. //constructor of the class
  12. public Car(int efficiency){
  13. this.efficiency=efficiency;
  14. this.fuel=0;
  15. }
  16.  
  17. //drive method implementation
  18. public void drive(int miles){
  19. //calculate fuel consumed
  20. int fuel_consumed=miles/efficiency;
  21. //update fuel
  22. fuel-=fuel_consumed;
  23. }
  24.  
  25. //method to add gas
  26. public void addGas(int gas){
  27. this.fuel+=gas;
  28. }
  29.  
  30. //method to get gas level
  31. public int getGasLevel(){
  32. return this.fuel;
  33. }
  34. }
  35.  
  36. //main class to test our Car class
  37. public class Main{
  38.  
  39. //main method implementation
  40. public static void main(String[] args) {
  41.  
  42. Car myHybrid = new Car(50); //50 miles per gallon
  43. myHybrid.addGas(20); //Tank 20 gallons
  44. myHybrid.drive(100); //Drive 100 miles
  45. System.out.println(myHybrid.getGasLevel()); //Print fuel remaining
  46. }
  47. }
Success #stdin #stdout 0.08s 46932KB
stdin
Standard input is empty
stdout
18