Problems in the implementation of methods

Asked

Viewed 136 times

3

I have the following classes:

Door

package meu.programa;
public class Porta {
    boolean aberta;
    String cor;
    double dimensaoX;
    double dimensaoY;
    double dimensaoZ;
    void abre() {
        if (aberta == false) {
            aberta = true;
        }
    }
    void fecha() {
        if (aberta == true) {
            aberta = false;
        }
    }       void pinta(String s) {
        cor = s;
    }
    boolean estaAberta() {
        if (aberta == true) {
            return true;
        }
        else {
            return false;
        }
    }
    void mostra() {
        System.out.println("A porta está aberta? " + this.estaAberta());
        System.out.println("A cor da porta eh: " + this.cor);
        System.out.println("A altura da porta eh: " + this.dimensaoY);
        System.out.println("O comprimento da porta eh: " + this.dimensaoX);
        System.out.println("A largura da porta eh: " + this.dimensaoZ);

    }
}

House

package meu.programa;
public class Casa {
    String cor;
    Porta[] portas;
    public Casa (int NumMaxDePortas) {
        this.portas = new Porta[NumMaxDePortas];
    }       
    void pinta(String s) {
        cor = s;
    }
    void quantasPortasEstaoAbertas() {
        int contador = 0;
        for(int i=0; i<this.portas.length; i++) {
            if (????) {
                contador++;
            }
        }
    System.out.println(contador);
    }

    void adicionaPorta(Porta p) {
        for(int i=0; i<this.portas.length; i++) {
            if(this.portas[i] == null) {
                this.portas[i] = p;
            }
        }
    }
    void totalDePortas (Casa casa) {
        int contador = 0;
        for(int i=0; i<this.portas.length; i++) {
            if(this.portas[i] != null) {
                contador++;
            }
        }
        System.out.println("Numero de portas: " + contador);
    }
}

Testacasa

package meu.programa;

public class TestaCasa {
    public static void main(String[] args) {
        Casa house = new Casa(30);
        house.cor = "Verde";
        Porta p1 = new Porta();
        Porta p2 = new Porta();
        Porta p3 = new Porta();

        house.adicionaPorta(p1);
        house.adicionaPorta(p2);
        house.adicionaPorta(p3);

        p1.abre();
        p2.fecha();
        p3.abre();
        p2.abre();

        house.quantasPortasEstaoAbertas();
        house.totalDePortas(casa);

    }
}

How would be the implementation of all the methods() ???

2 answers

5

You just played your code and did not explain limitations. The why of the structures used and asked to explain how to implement two methods. I will give a summary of the problems as well as an example of how it should be done.

1. Array of Port

With the following code: portas = new Porta[NumMaxDePortas]; you are creating a vector of doors with the maximum size which will make you have all these doors whenever you create the house and will not be limiting a maximum size for it. This implies that you are using more memory than you need to do so and that you will have a large vector to go through. Since it does not store the amount of initialized ports.

If you really want to use an Array (array) of ports, you need to also have an integer variable to know the actual amount of ports you currently have.

The ideal thing for this case would be to use lists, as this would facilitate the rest of the logic of your code, since it would allow you to work dynamically and simplifying the necessary logic.

2. Methods do not follow the recommended standardisation

Instead of pinta() should use a default name to identify what happens in the class, as the field you are changing has color name, the recommended would be:

void setCor(String cor) {
    this.cor = cor;
}

In addition to the methods created not explicit which the visibility of it making us Package-Private which is a very specific type of visibility which is not recommended for most cases.

The methods are all of the type void and you print directly on them, while you should print only the results obtained by calling them and in the place where they were called.

3. General logical problems

In the method below, you check if the open variable is true and if it is, it returns true. Your code will be much more efficient and will make more sense if you simply return the variable itself, there is no reason to check if the return will be equal to the value of the same.

boolean estaAberta() {
    if (aberta == true) {
        return true;
    }
    else {
        return false;
    }
}

Another example of logical problem is the following method:

void abre() {
    if (aberta == false) {
        aberta = true;
    }
}

Look what you did, if the door is closed you open and do nothing if it is already open and the return of the method is void. This verification would make sense only if the method informed the user if the door was opened (because it was closed). But if the goal is just to leave it open if calling could do so:

public void abre() {
        aberta = true;
}

What saves a logical operation on your processor.

I created a version of this code following the recommendations I mentioned and put them in the following gist.

3

His method totalDePortas is almost good: the only thing left is this parameter casa. If the method already belongs to the class Casa - and you can access the object in question through the keyword this (as you are already doing) - there is no need to pass a another box as parameter.

As for the method quantasPortasEstaoAbertas, what you need to do is get a reference for each port stored, and then you can call methods and/or access properties in that object Porta:

for(int i=0; i<this.portas.length; i++) {
    Porta p = this.portas[i]; // Pode ser null - lembrar de testar
    ...
    String corDaPorta = c.cor; // Exemplo de propriedade
    boolean portaAberta = p.estaAberta(); // Exemplo de método

From there I believe you can complete the exercise on your own. If that wasn’t your question, please edit the question and clarify what is causing you difficulties.

Note: In practice, it is good to avoid (in Java) accessing properties directly, using access methods instead (getters and setters). The answer of Kyllopardiun gives an example of a Setter (setCor; could also be atribuirCor, if you want maintain the code in English). However, if you are not yet familiar with visibility modifiers (public, protected, private) You don’t have to worry about that for the time being.

Browser other questions tagged

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