Java help with the concept of how to relate classes

Asked

Viewed 425 times

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

    Conceptually a house is a building. Is this the intention? Is the name correct? Or did you really want to use that name for a building with floors? Are they really different things? Other than that, I don’t understand what your problem is? What’s your question? What would "use the house attribute to implement the building"? You’re really schist Casa have 3 variables for doors and Edificio have a array (yet used in the wrong way).

  • Create the Building class with the Attributes: color, totalDertas, totalDeAndares, ports[] and methods: void pinta(String color), int quantasPortasSports(), void addPorta(Port p), int totalDePortas(), void addAndar(), int totalDeAndares() To test, create a building, paint the. Create six doors and place them in the building using the added method.

  • I repeat, I see no doubt there.

  • Bigown, my php and c teachers, went through a lot of stuff and the java step class heritage, after the building I still have to create a still class as parent class and put the house and the building as son, I’m trying to fit it in the head, because I have proof of that today, if you already have content on this and can pass me thank you, :))))

  • bigown my doubt and that, in the house I used port 1 = new door(); to create doors in the house and to create doors in the building, I am not understood

  • In the class door I had a fixed number of attribute as door, were 3 doors, already in the class building that amount are n doors. how to do ? :/

  • First, I should have created the class Imovel first. Then it’s gonna be both. So somehow any answer to this question may be useless depending on what changes next. This is all still too confusing. I prefer not to answer because the base is all wrong. You have to start again by doing the right thing. Then who knows might not mix the object with the GUI. This is another conceptual error that complicates also.

  • in the case Voce advises me to go from the end to the beginning ? because the sequence that my teacher recommended was create class door - use main method to test door class methods like open, close and paint. create house class - declare door attribute , referencing the door class , create 3 doors and use methods to open and paint on it. create building class - ? to doubt this already with attributes

  • 1

    The class Porta is independent. The problem is starting an inheritance by the child classes, this does not usually work, especially without experience.

  • class edificio -- my teacher recommend, so that I create a void method Addirporta(Port p ){}; And then I have the attribute gates[]; // which is an array and it will pass parameter so that I can add the gates ... and that the concept ? mustache ?

  • I understood the door will not have relationship, only immobile , house and building (building);

  • Relationship will, will not inherit.

  • bigown I can create a variable n , and ask the user to enter the number of doors of the building and then I do a for 1 < n and put so that doors[] have n elements, so I will have the number of doors q the user wants, right ?

  • this bigown rsrs what I meant, still confusing relationship with heritage. rsrs

  • 1

    Right. You can do this.

  • I’ll try to do, thanks brother :))

  • can take a look bigown and see if this good ?

Show 12 more comments

1 answer

1


I would solve this problem there another way.

Door-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 don’t put any, the java compiler puts a blank.

The way it is now, even when you instantiate the Port class with the default constructor, you won’t run the risk of raising Nullpointer, because the default constructor already initializes the variables before you start using them.

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

This thing you did doesn’t make any sense, and it’s a very common mistake when learning to program. It works, but it’s very ugly. 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 just return it? It is true or false, if it is open it will return true if it is closed it will return False, it is over, you do not need to do all that you did.

if(aberta == true)...

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

if(aberta)...

If opened is true, it will enter if, if it is not, it will not.

Edificio 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 does not make any sense, since you can get it with the size of Arraylist. Unless it means the maximum number of doors, but since you didn’t say anything, I don’t think.

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

That I’ve commented on before.

Casa Class:

This is the wrong part of your code. The most important things in Object Orientation is Polymorphism and Heritage.

House is a kind of Building, so I can say that the Building class is the mother of House.

My House class is all there. Look at you:

public class Casa extends Edificio {

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

Now go to Main there and do it:

Edificio casa = new Casa();

casa.pinta("Laranja");

System.out.println(casa.cor);

The result will be: Laranja

Magic? No, it’s Polymorphism + Heritage.

Do you know why this happened? Because as a house is a kind of building, I extended the Class to Building. ALL, I SAID ALL, the attributes and methods of the Building class are now also House Class without you having to write in Home.

I recommend that you play there with the classes and see the size of the power of polymorphism and heritage.

Any questions just ask.

Browser other questions tagged

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