Problems in implementing methods

3

I have the following classes:

Port

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

    }
}

Home

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

    }
}

What would be the implementation of the methods HowDonthlyDownloads () and totalDownloads () ???

    
asked by anonymous 18.08.2014 / 03:19

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. Port Array

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

If you really want to use a Port Array, you also need to have an integer variable to know the actual number of ports you currently have.

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

2. Methods do not follow recommended standardization

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

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

In addition to methods created not explicit what 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 type void and you print directly to them, while you should print only the results you get when you call them and where they were called.

3. Generalized Logical Problems

In the method below, you check whether the open variable is true and if it is, 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 a logical problem is the following method:

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

See what you did, if the port is closed you open it and do nothing if it is already open and the method return is void . It would make sense for this check only if the method informs the user if the port 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 made and put them in the following gist.

    
18.08.2014 / 06:55
3

Your totalDePortas method is almost good: the only thing that is left over is this casa parameter. If the method already belongs to the class Casa - and you can access the object in question through the keyword this (as you already do) - there is no need to pass an other house as parameter.

As for the quantasPortasEstaoAbertas method, you need to get a reference for each port stored, and then you can call methods and / or access properties on 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 by yourself. If your question was not, please edit the question and clarify what is causing you difficulties.

Note: In practice, it is good to avoid accessing properties directly, using accessor methods instead ( getters and setters ). The Kyllopardiun answer gives an example of a setter ( setCor ; could be also atribuirCor , if you want keep the code in Portuguese ). However, if you're not already familiar with visibility modifiers ( public , protected , private ) you do not have to worry about it for now.     

18.08.2014 / 11:44