Improve your vehicle

Report a typo

Add the field int horsePower to the class Engine.

Then create a method void printHorsePower inside the inner class that prints the name of your Vehicle and the horsePower of it's Engine.

For a vehicle called Dixi with 20 horsepower the output of printHorsePower should be:

Vehicle Dixi has 20 horsepower.

And don't forget to add constructors in both classes!

Please, don't use access modifier private for methods.

Write a program in Java 17
class Vehicle {

private String name;

// create constructor

class Engine {

// add field horsePower
// create constructor

void start() {
System.out.println("RRRrrrrrrr....");
}

// create method printHorsePower()
}
}

// this code should work
class EnjoyVehicle {

public static void main(String[] args) {

Vehicle vehicle = new Vehicle("Dixi");
Vehicle.Engine engine = vehicle.new Engine(20);
engine.printHorsePower();
}
}
___

Create a free account to access the full topic