Can anyone give me a notion of how I do [closed]

-3

Compute the representation of the operation of a lamp, where it is possible to switch on, switch off and check that the lamp accesses

I did this:

class Lampada {


  boolean aceso = true;


  void apaga() {
    aceso = false; 
  }


  void acende() {
    aceso = true; 
  }


  boolean estaApagada() {
    if (aceso == false) {
      return true;
    }
    else { 
      return false;
    }
  }

}
    
asked by anonymous 06.09.2017 / 00:37

1 answer

2

The class looks ready, but an improvement can be made in the estaApagada() method:

  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 visibility to the% change% with%. Now if it needs two methods to be changed, it is not appropriate to leave it with a possible external access, adding aceso you restrict the direct access to it.

Another detail cited by #

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;
    }
}

Here are some links to the site that can help you learn more about basic java and object-oriented concepts:

What's 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 they and what are their main differences?

    
06.09.2017 / 01:04