- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
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
