Can someone give me a sense of how I do

Asked

Viewed 76 times

-3

Model computationally the representation of the functioning of a lamp, where it is possible to light, erase and check whether the lamp is access.

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

}
  • From the code you just showed, it looks done. No?

  • 1

    Does a lamp already start? That’s right?

  • 1

    The method estaApagada() can be summarized to only one line: return !aceso;

  • 1

    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.

1 answer

2

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?

  • As you pointed out yourself, it is also strange that the lamp starts to light. Incidentally this check is off is also strange. If the property is aceso, the most natural question (in my head) would be boolean estaAcesa, who would return aceso, which in turn would be private. Anyway, if you want to mess with the code there’s a lot of stuff, but the question isn’t clear at all about what you want.

  • @bfavaretto thought about these things, but had found that, for lack of clarity, it would not be necessary to comment, but is right, for seeming to be a beginner, it is good to mention these problems.

  • 1

    Yeah, my comment up there ("... seems done. No?") was kind of to see if he says where he wants to go. But he didn’t say...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.