DEV Community

SIVAGANESH L
SIVAGANESH L

Posted on

Calculating Bike Mileage in simple Java program

create a simple Java class called Bike to calculate mileage based on distance traveled and fuel consumed.

What is Mileage?
Mileage is nothing but how much distance a vehicle is travelled consuming certain amount of fuel. Its calculated as
mileage = distance traveled / fuel used.
EX: 150/5 = 30km/l

The program has:
A method to get distance travelled
A method to calculate mileage

public class Bike {

public int getDistance(int distance){

    return distance;
}

public int getMileage(int distance, int fuelQuantity) {

    return distance/fuelQuantity;
}

public static void main(String[] args) {
    Bike myBike = new Bike();
    int distance = myBike.getDistance(300);
    int mileage = myBike.getMileage(distance, 5);
    System.out.println("Mileage of this bike:" +mileage);
    }
         }
Enter fullscreen mode Exit fullscreen mode

getDistance(): This method return the distance value
getMileage(): This method calculate the mileage using the formula
main(): create a Bike object myBike in main method and input the value for distance and fuelQuantity and prints the mileage.

Top comments (0)