The class seems ready, but an improvement can be made in the method estaApagada()
:
boolean estaApagada() {
return !aceso;
}
This will return the opposite of the variable on, which is exactly the status expected by the method.
There are other things that can be improved, such as the fact that you do not set a more restricted to variable visibility aceso
. Now if it needs two methods to be changed, it is not appropriate to leave it with a possible external access, adding private
you restrict the direct access to it.
Another detail cited by @Douglas and that makes total sense is the statement ask to be checked if the lamp is lit, and your method checks if it is out. Although it is also possible to make such a check with the method created, the method seems inconsistent with the one requested in the statement. The correction is as simple as the current method, just return the variable itself aceso
.
The class with the applied suggestions would look like this:
class Lampada {
private boolean aceso = true;
public void apaga() {
aceso = false;
}
public void acende() {
aceso = true;
}
public boolean estaAceso(){
return aceso;
}
}
I leave below some links here from the site itself that can help you learn more about basic java and object orientation concepts:
What is the difference between public, default, protected and private modifiers?
What are the concepts of cohesion and coupling?
Functional Programming and Object-Oriented Programming. What are and what are their main differences?
From the code you just showed, it looks done. No?
– bfavaretto
Does a lamp already start? That’s right?
– user28595
The method
estaApagada()
can be summarized to only one line:return !aceso;
– user28595
The statement says "check if the lamp is lit", this method is checking if the lamp is out:
estaApagada()
, Seems a little incoherent even though it works.– Douglas