Vehicle Speed Calculator in Java: Explanation & Practice
Calculate max speeds for different vehicles
Problem summary
Create Vehicle hierarchy where each type has different speed calculation based on attributes.
Starter code
// Vehicle: abstract getMaxSpeed()
// Car: horsePower * 0.5
// Motorcycle: horsePower * 0.7
// Bicycle: gears * 5
public class Main {
public static void main(String[] args) {
// Test case 1: Car with 200 HP
// Print: Max speed: <result> km/h
}
}Expected output and test cases
- Car 200 HP
Max speed: 100.0 km/h
- Motorcycle 200 HP
Max speed: 140.0 km/h
- Bicycle 21 gears
Max speed: 105.0 km/h
Hints
- Each vehicle calculates speed differently
- Store relevant attributes in each class
- Override getMaxSpeed() method
Related Inheritance exercises
- Practice Shape Area Calculator in Java
- Practice Payment Processing System in Java
- Practice Bank Account Types in Java
Practice all Inheritance exercises · Run this idea in the Java compiler