Write a complete Java class definition (without a main method) for class Locomot
ID: 3934795 • Letter: W
Question
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.
Explanation / Answer
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.