Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Input in Java

Chapter 6

Input in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Commute Cost Calculator


Write a program that takes the distance of the commute in kilometres, the car fuel consumption rate in kilometre per gallon, and the price of a gallon of petrol as input. The program should then display the cost of the commute.

import java.util.Scanner;

 

public class CommuteCostCalculator {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Input values

        System.out.print("Enter distance of commute (in km): ");

        double distance = sc.nextDouble();

 

        System.out.print("Enter car fuel consumption (km per gallon): ");

        double mileage = sc.nextDouble();

 

        System.out.print("Enter price of petrol per gallon: ");

        double pricePerGallon = sc.nextDouble();

 

        // Calculations

        double gallonsRequired = distance / mileage;

        double totalCost = gallonsRequired * pricePerGallon;

 

        // Output

        System.out.println("\nCost of the commute = Rs. " + totalCost);

 

        sc.close();

    }

}

Output

 
SAMPLE INPUT : 
Enter distance of commute (in km): 40
Enter car fuel consumption (km per gallon): 20
Enter price of petrol per gallon: 350

SAMPLE OUTPUT : 
Cost of the commute = Rs. 700.0

Explanation

Java Programs