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

  1. Each vehicle calculates speed differently
  2. Store relevant attributes in each class
  3. Override getMaxSpeed() method

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler