Write a complete Java class definition without a main method
Write a complete Java class definition (without a main method) for class Locomotive. Each object of this class has the following data fields:
model: a String value representing the type of locomotive, e.g., “EMD F40PH”.
power: an integer value indicating the maximum horsepower, e.g., 26000.
Your class should also define the following member methods:
A constructor that accepts a String parameter representing the model of the locomotive.
This constructor should initialize power to 0.
A mutator method that sets the maximum horsepower to a specified integer value
An accessor method that returns the maximum horsepower.
Solution
Locomotive.java
public class Locomotive {
private String model;
private int power;
public Locomotive(String locomotive){
model = locomotive;
power = 0;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
