Marcel :
I want to write a method that changes the return statement of a different method. For example, I have 3 methods:
openCar
closeCar
isTheCarOpen:boolean
So if a user calls the method openCar
and then calls the method isTheCarOpen
it should return true
.
public void openCar() {
System.out.println("Car has been opened");
}
public void closeCar() {
System.out.println("Car has been closed");
}
public boolean isTheCarOpen() {
return false;
}
Federico klez Culloca :
You don't change the return statement. You change what the method returns. In your case, you want to have a boolean in the class representing the state of the door (let's call it doorOpened
) and you set its value when you call the openCar
and closeCar
method.
class Car {
private boolean doorOpened;
public void openCar() {
doorOpened = true;
System.out.println("Car has been opened");
}
public void closeCar() {
doorOpened = false;
System.out.println("Car has been closed");
}
public boolean isTheCarOpen() {
return doorOpened;
}
}