Help in java with concept of how to relate classes

0
package br.edu.utfpr.exer05;

public class Porta {
    boolean aberta;
    String cor;
    double dimensaoX, dimensaoY, dimensaoZ;

    void abre() {
        this.aberta = true;
    }
    void fecha() {
        this.aberta = false;
    }
    void pinta(String cor) {
        this.cor = cor;
    }
    boolean estaAberta() {
        boolean estaAberta = false;
        if(aberta == true) estaAberta = true;
        return estaAberta;
    }

}

package br.edu.utfpr.exer05;

import java.util.ArrayList;

public class Edificio {
    String cor;
    int totalDePortas;
    int totalDeAndares;
    ArrayList<Porta> portas = new ArrayList<Porta>();

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

    int quantasPortasEstaoAbertas() {
        int qtdPortasAbertas = 0;
        for(int i=0; i<portas.size(); i++) {
            portas.get(i);
            if(portas.get(i).estaAberta() == true) {
                qtdPortasAbertas++;
            }
        }
        return qtdPortasAbertas;
    }

    void adicionaPorta(Porta porta) {
        this.portas.add(porta);
    }

    int totalDePortas() {
        return this.portas.size();
    }

    void adicionaAndar() {
        this.totalDeAndares = totalDeAndares += 1;
    }

    int totalDeAndares() {
        return this.totalDeAndares;
    }
}

package br.edu.utfpr.exer05;

public class Casa {
    String cor;
    Porta porta1, porta2, porta3;

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

    int quantasPortasEstaoAbertas() {
        int qtdPortasAbertas=0;
        if(porta1.estaAberta() == true) qtdPortasAbertas++;
        if(porta2.estaAberta() == true) qtdPortasAbertas++;
        if(porta3.estaAberta() == true) qtdPortasAbertas++;
        return qtdPortasAbertas;
    }

    int totalDePortas() {
        return 3;
    }
}
    
asked by anonymous 24.06.2016 / 19:10

1 answer

1

I would solve this problem there otherwise.

Port class:

public class Porta {
    boolean aberta;
    String cor;
    double dimensaoX, dimensaoY, dimensaoZ;

    public void Porta(boolean aberta, String cor, double dimensaoX, double dimensaoY, double dimensaoZ){
        this.aberta = aberta;
        this.cor = cor;
        this.dimensaoX = dimensaoX;
        this.dimensaoY = dimensaoY;
        this.dimensaoZ = dimensaoZ;
    }

    public void Porta(){
        //Atributos padrões apenas para inicialização. Poderiam ser quaisquer outros.
        aberta = false;
        cor = "azul";
        dimensaoX = 2.00;
        dimensaoY = 1.00;
        dimensaoZ = 0.5;
    }

    void abre() {
        this.aberta = true;
    }

    void fecha() {
        this.aberta = false;
    }

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

    boolean estaAberta() {
        return aberta;
    }
}

It is highly recommended that you always have a constructor to initialize the variables. You can have more than one constructor, when you do not put any, the java compiler places a void.

The way you are now, even when you instantiate the Port class with the default constructor, you will not run the risk of raising NullPointer, since in the default constructor you are already initializing the variables before you start using them.

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

What you've done does not make the slightest sense, and it's a very common mistake when you're learning to program. It works, but it's very ugly. Do you want a method that tells you whether the door is open or not and you have an 'open' variable that tells you this, now, why not simply return it? It is true or false, if it is open it will return true if it is closed it will return False, it's over, you do not have to do all that you did.

if(aberta == true)...

This is also very ugly and extremely unnecessary. The variable 'open' is already a boolean, does not have to be compared with false or true, because it already has one of two values, it would just do:

if(aberta)...

If opened is true, it will go into if, otherwise it will not.

Building Class:

public class Edificio {
    String cor;
    int totalDeAndares;
    ArrayList<Porta> portas;

    public void Edificio(String cor, int totalDeAndares, ArrayList<Porta> portas){
        this.cor = cor;
        this.totalDeAndares = totalDeAndares;
        this.portas = portas;
    }

    public void Edificio(){
        //Valores padrao.
        portas = new ArrayList<>();
        cor = "azul";
        totalDeAndares = 1;
    }

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

    int quantasPortasEstaoAbertas() {
        int qtdPortasAbertas = 0;
        for(int i=0; i<portas.size(); i++) {
            if(portas.get(i).estaAberta()) {
                qtdPortasAbertas++;
            }
        }
        return qtdPortasAbertas;
    }

    void adicionaPorta(Porta porta) {
        this.portas.add(porta);
    }

    int totalDePortas() {
        return this.portas.size();
    }

    void adicionaAndar() {
        this.totalDeAndares++;
    }

    int totalDeAndares() {
        return this.totalDeAndares;
    }
}

The total attribute of Ports does not make any sense, since you can get with the size of the ArrayList. Unless it means the maximum number of ports, but since you did not say anything, I do not think so.

if(portas.get(i).estaAberta() == true)...

That's what I've discussed before.

House Class:

This is the wrong part of your code. The most important things in Object Guidance are Polymorphism and Inheritance.

Home is a type of Building, so I can say that the Building class is Mother of Home.

My Home Class is all there. Look:

public class Casa extends Edificio {

    public void Casa(){
        super.Edificio();
    }
}

Now go on Main and do this:

Edificio casa = new Casa();

casa.pinta("Laranja");

System.out.println(casa.cor);

The result will be: Laranja

Magic? No, it's Polymorphism + Inheritance.

Do you know why this happened? Because as house is kind of building, we extended the Class each to Building. EVERYONE, I said EVERYONE, the attributes and methods of the Building class are now also House Class without you needing to write at Home.

I recommend you to play there with classes and see the size of polymorphism and inheritance power.

Any questions just ask.

    
26.06.2016 / 02:34