Take an attribute from another class and put as parameter in a constructor

Asked

Viewed 104 times

-1

I have a tremendous doubt that I can’t find a solution!

I’m trying to get a constructor to take an attribute from a class and make it as a parameter as shown in the following code:

public class Comprador { 

    private String nome;
    private int idade;
    private EnderecoResidencial endereco;


    public Comprador(String nome, int idade, EnderecoResidencial endereco) {
        this.nome = nome;
        this.idade = idade;
        this.endereco = endereco;
    }

    public Comprador(String nome, int idade) {
        this.setNome(nome);
        this.setIdade(idade);   
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public EnderecoResidencial getEndereco() {
        return endereco;
    }

    public void setEndereco(EnderecoResidencial endereco) {
        this.endereco = endereco;
    }

    public void imprime() {

        System.out.println("Nome: " + getNome());
        System.out.println("Idade: " + getIdade());
        System.out.println("Endereço: " + getEndereco());

    }

}
public class EnderecoResidencial {

    private String enderecoCompleto;

    public EnderecoResidencial(String endereco) {
        this.enderecoCompleto = endereco;
    }

    public String getEnderecoCompleto() {
        return enderecoCompleto;
    }

    public void setEnderecoCompleto(String enderecoCompleto) {
        this.enderecoCompleto = enderecoCompleto;
    }

    public void imprime() {
        System.out.println("O endereço completo é: " + getEnderecoCompleto());

    }

The problem is that doing this in main is generating the error: (The constructor Buyer(String, int, String) is Undefined.) Only in the constructor in which I want to get the address.

public class Teste {

    public static void main(String[] args) {

        EnderecoResidencial enderecoresidencial = new EnderecoResidencial("Rua Pauliceia");
        enderecoresidencial.imprime();

        Comprador comprador1 = new Comprador("Ari", 22, ""); //(The constructor Comprador(String, int, String) is  undefined.)
        comprador1.imprime();

        Comprador comprador2 = new Comprador("Ana", 18);
        comprador2.imprime();

        System.out.println(comprador1.getEndereco());

        System.out.println(enderecoresidencial.getEnderecoCompleto());  
    }

}

What am I wrong with this code? The address parameter in the constructor is wrong?

1 answer

1

Error because you are sending a string as a parameter instead of the object. Try:

Comprador comprador1 = new Comprador("Ari", 22, enderecoresidencial);

Browser other questions tagged

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