I am learning how to write neat and organized code in Java. Can a set()
method return a value or is there a more efficient/readable way of doing this?
public class Car {
private boolean mHasAxles;
private boolean mHasTires;
private Tires mTires;
public setAxels(boolean hasAxles) {
mHasAxels = hasAxles;
}
public boolean hasAxles() {
return mHasAxles;
}
public boolean setTires(Tires tires) {
if(hasAxles()){
mTires = tires;
mHasTires = true;
return true; // Returns true if it was able to set tires
}
return false; // Returns false because car did not have axels
// Therefore, tires could not be set
}
}
In this example, my question is specifically about the setTires()
method. Should the class check whether the car has axles when setting the tires or should the logic be left to the class that uses Car
? Should the setTires()
method be called something else since it returns a value?