Check Driving Eligibility in Java
Problem
Driving eligibility depends on both the vehicle category and a minimum age tied to that category, so the check has to combine two pieces of information instead of comparing a single value.
Given a vehicle type and a person's age, determine whether they meet the minimum age required to drive that type of vehicle.
Java Program
public class DrivingEligibility {
public static void main(String[] args) {
String vehicleType = "car";
int age = 19;
boolean eligible;
if (vehicleType.equals("two-wheeler without gear")) { // equals(), not ==, for String content comparison
eligible = age >= 16;
} else if (vehicleType.equals("car") || vehicleType.equals("two-wheeler with gear")) {
eligible = age >= 18;
} else if (vehicleType.equals("commercial vehicle")) {
eligible = age >= 20;
} else {
eligible = false; // unrecognized vehicle type
}
System.out.println("Eligible to drive " + vehicleType + ": " + eligible);
}
}Output
Core Logic
Matching the vehicle type against each category in turn, and comparing the age against that category's minimum, covers every case with a single chain of checks.
- 1
vehicleType.equals(...)is used instead of==, since string content — not object identity — is what matters here. - 2"two-wheeler without gear" requires only
age >= 16, the lowest threshold in this scheme. - 3"car" and "two-wheeler with gear" are grouped under the same
age >= 18check using||, since they share a minimum age. - 4"commercial vehicle" requires
age >= 20, and any unrecognized vehicle type falls through tofalse.
vehicleType = "car" and age = 19, the second branch matches and 19 >= 18 evaluates to true.Key Point: This illustrative age scheme is for demonstrating multi-branch conditionals, not a statement of any real jurisdiction's actual licensing law.
Key Concepts
Approach 2: Using Switch Statement
public class DrivingEligibilitySwitch {
public static void main(String[] args) {
String vehicleType = "car";
int age = 19;
int minimumAge;
switch (vehicleType) {
case "two-wheeler without gear":
minimumAge = 16;
break;
case "car":
case "two-wheeler with gear": // stacked labels fall through to the same assignment
minimumAge = 18;
break;
case "commercial vehicle":
minimumAge = 20;
break;
default:
minimumAge = Integer.MAX_VALUE; // unknown vehicle type is never eligible
}
boolean eligible = age >= minimumAge;
System.out.println("Eligible to drive " + vehicleType + ": " + eligible);
}
}
Output
Core Logic
A switch statement can look up each vehicle type's minimum age directly, separating 'what age is required' from 'does this person meet it'.
- 1
switch (vehicleType)matches the string against eachcaselabel in turn. - 2"car" and "two-wheeler with gear" share a
caseblock by stacking their labels together, so both fall through to the sameminimumAge = 18assignment. - 3The
defaultcase assignsInteger.MAX_VALUEas the minimum age, guaranteeing an unrecognized vehicle type is never treated as eligible. - 4Once
minimumAgeis resolved, a single comparison —age >= minimumAge— determines eligibility, the same as the if/else version's final check.
vehicleType = "car", the switch resolves minimumAge to 18, and 19 >= 18 evaluates to true.Key Point: Stacking case labels without a break between them is what lets "car" and "two-wheeler with gear" share one assignment — separating the lookup from the comparison keeps each part of the logic simple.