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

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Menu Driven Program - Electric Taxi Fare Calculator


42. A new taxi service based on electric vehicles is offering services within a metro city. Following is the fare chart including the type of taxi used to commute and price per kilometer. Create a program to calculate total fare depending on the distance travelled in kilometers.

Type

Fare

Micro

10.05/Km

Macro

15.05/Km

Shared

7.05/Km

import java.util.Scanner;

 

public class ElectricTaxiFare {

    public static void main(String[] args) {

 

        // Title

        System.out.println("ELECTRIC TAXI FARE CALCULATOR");

        System.out.println("-----------------------------");

 

        Scanner sc = new Scanner(System.in);

 

        double distance, farePerKm = 0, totalFare;

        int choice;

 

        System.out.println("Select Taxi Type:");

        System.out.println("1. Micro");

        System.out.println("2. Macro");

        System.out.println("3. Shared");

        System.out.print("Enter your choice: ");

        choice = sc.nextInt();

 

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

        distance = sc.nextDouble();

 

        switch (choice) {

            case 1:

                farePerKm = 10.05;

                break;

 

            case 2:

                farePerKm = 15.05;

                break;

 

            case 3:

                farePerKm = 7.05;

                break;

 

            default:

                System.out.println("Invalid taxi type selected!");

                sc.close();

                return;

        }

 

        totalFare = farePerKm * distance;

 

        System.out.println("Fare per Km   : Rs. " + farePerKm);

        System.out.println("Total Fare    : Rs. " + totalFare);

 

        sc.close();

    }

}

Output

Sample Output 

ELECTRIC TAXI FARE CALCULATOR
-----------------------------
Select Taxi Type:
1. Micro
2. Macro
3. Shared
Enter your choice: 2
Enter distance travelled (in km): 12
Fare per Km   : Rs. 15.05
Total Fare    : Rs. 180.6

📝 Explanation

  • User selects taxi type from the menu
  • User enters distance travelled
  • switch-case assigns the correct fare per km
  • Total fare is calculated using:
  • Total Fare = Distance × Fare per Km
  • Error message is shown for invalid choice