1
I’m having trouble connecting Telefone
with class Pessoa
, a person can have up to three phones. How does this?
package model;
public class Pessoa {
private String nome;
private String endereco;
private String sobrenome;
private Telefone tels[];
public Telefone[] getTels() {
return tels;
}
public void setTels(Telefone[] tels) {
this.tels = tels;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
}
Telephone class:
package model;
public class Telefone {
private String telefone;
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
test class:
package model;
public class Teste {
public static void main(String[] args) {
Pessoa pessoa = new Pessoa();
pessoa.setNome("Aline");
pessoa.setSobrenome("Gonzaga");
pessoa.setEndereco("Bairro Barra");
String telefones[] = new String[3];
telefones[0] = "7627-86476";
telefones[1] = "5362-56423";
telefones[2] = "33333-3333";
System.out.println(pessoa.getNome());
System.out.println(pessoa.getSobrenome());
System.out.println(pessoa.getEndereco());
for(int i = 0; i < 3; i++){
System.out.println();
}
}
}
Start the constructor of
Pessoa
withthis.tels = new Telefone[3]
. Thus, it will only be possible to add 3 phones. If you try to add more, aIndexOfBoundException
which is the attempt to access or add an invalid Input in a pre-defined array.– user28595
I did this in the Person builder, put the tels = new Phone[3]. But in the main class I created a Telephone type vector with 4 more elements and rotated...
– Aline