Skip to content

Commit 4986b03

Browse files
committed
exercício 2 da POO feito
1 parent 9fcf10d commit 4986b03

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

PooExercicio2/Agenda.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
package PooExercicio2;
22
import java.util.Scanner;
33

4+
import javax.swing.JOptionPane;
5+
46
public class Agenda {
57
Scanner sc = new Scanner(System.in);
68
// atributos
79
String nome;
8-
float altura;
10+
double altura;
911
int idade;
1012

1113
// métodos
14+
// construtor com parâmetros
15+
public Agenda(String nome, double altura, int idade) {
16+
this.nome = nome;
17+
this.altura = altura;
18+
this.idade = idade;
19+
}
20+
21+
// construtor vazio
22+
public Agenda() {
23+
24+
}
25+
26+
// getters and setters
1227
public String getNome() {
1328
return nome;
1429
}
@@ -17,11 +32,11 @@ public void setNome(String nome) {
1732
this.nome = nome;
1833
}
1934

20-
public float getAltura() {
35+
public double getAltura() {
2136
return altura;
2237
}
2338

24-
public void setAltura(float altura) {
39+
public void setAltura(double altura) {
2540
this.altura = altura;
2641
}
2742

@@ -33,40 +48,11 @@ public void setIdade(int idade) {
3348
this.idade = idade;
3449
}
3550

36-
public void armazenaPessoa() {
37-
int cont=0;
38-
39-
while(cont < 2) {
40-
System.out.println("Informe o seu nome: ");
41-
setNome(sc.next());
42-
System.out.println("Informe a sua idade: ");
43-
setIdade(sc.nextInt());
44-
System.out.println("Informe a sua altura: ");
45-
setAltura(sc.nextFloat());
46-
47-
cont++;
48-
}
49-
}
50-
51-
public void buscaPessoa(){
52-
String buscaPessoa;
53-
System.out.println("Informe o nome da pessoa que quer procurar: ");
54-
buscaPessoa = sc.next();
55-
56-
for (int i = 0; i < 2; i++) {
57-
if(getNome().equals(buscaPessoa)){
58-
System.out.println("Esta pessoa está na posição: " + (i+1));
59-
}
60-
}
61-
}
62-
63-
public void imprimeAgenda() {
64-
System.out.println("Dados das pessoas das agendas: ");
51+
// métodos de aplicações dievrsas
52+
// void
53+
6554

66-
for (int i = 0; i < 2; i++) {
67-
System.out.println(getNome());
68-
System.out.println(getIdade());
69-
System.out.println(getAltura());
55+
public void imprimir() {
56+
JOptionPane.showMessageDialog(null, "Nome: " + nome + "\n Altura: " + altura + "\nIdade: " + idade);
7057
}
71-
}
72-
}
58+
}

PooExercicio2/App.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
package PooExercicio2;
22

3+
import java.util.Random;
4+
35
import javax.swing.JOptionPane;
46

57
public class App {
68
public static void main(String[] args) {
7-
Agenda obj1 = new Agenda();
8-
obj1.armazenaPessoa();
9-
obj1.imprimeAgenda();
10-
9+
Random rd = new Random();
10+
// criar um vetor para 10 objetos
11+
Agenda contatos[] = new Agenda[10];
12+
//criar os objetos e preencher as informações
13+
for (int i = 0; i < contatos.length; i++) {
14+
// criar o objeto - construtor
15+
contatos[i] = new Agenda();
16+
// preencher os atributos
17+
contatos[i].setNome(JOptionPane.showInputDialog("Digite o nome: "));
18+
contatos[i].setAltura(rd.nextInt(200)/100);
19+
contatos[i].setIdade(i + 18);
20+
}
21+
22+
// criar um método para achar a pessoa do Array
23+
String buscaNome = JOptionPane.showInputDialog("Informe o nome a ser buscado");
24+
boolean busca = true;
25+
int cont=0;
26+
27+
while(busca) {
28+
if(buscaNome.equals(contatos[cont].getNome())){
29+
busca = false;
30+
contatos[cont].imprimir();
31+
}
32+
cont++;
33+
}
1134
}
1235
}

0 commit comments

Comments
 (0)