-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathOrdenacaoNumeros.java
More file actions
74 lines (60 loc) · 1.9 KB
/
OrdenacaoNumeros.java
File metadata and controls
74 lines (60 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main.java.list.Ordenacao;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class OrdenacaoNumeros {
//atributos
private List<Integer> numerosList;
//construtor
public OrdenacaoNumeros() {
this.numerosList = new ArrayList<>();
}
public void adicionarNumero(int numero) {
this.numerosList.add(numero);
}
public List<Integer> ordenarAscendente() {
List<Integer> numerosAscendente = new ArrayList<>(this.numerosList);
if (!numerosList.isEmpty()) {
Collections.sort(numerosAscendente);
return numerosAscendente;
} else {
throw new RuntimeException("A lista está vazia!");
}
}
public List<Integer> ordenarDescendente() {
List<Integer> numerosDescendente = new ArrayList<>(this.numerosList);
if (!numerosList.isEmpty()) {
numerosDescendente.sort(Collections.reverseOrder());
return numerosDescendente;
} else {
throw new RuntimeException("A lista está vazia!");
}
}
public void exibirNumeros() {
if (!numerosList.isEmpty()) {
System.out.println(this.numerosList);
} else {
System.out.println("A lista está vazia!");
}
}
public static void main(String[] args) {
// Criando uma instância da classe OrdenacaoNumeros
OrdenacaoNumeros numeros = new OrdenacaoNumeros();
// Adicionando números à lista
numeros.adicionarNumero(2);
numeros.adicionarNumero(5);
numeros.adicionarNumero(4);
numeros.adicionarNumero(1);
numeros.adicionarNumero(99);
// Exibindo a lista de números adicionados
numeros.exibirNumeros();
// Ordenando e exibindo em ordem ascendente
System.out.println(numeros.ordenarAscendente());
// Exibindo a lista
numeros.exibirNumeros();
// Ordenando e exibindo em ordem descendente
System.out.println(numeros.ordenarDescendente());
// Exibindo a lista
numeros.exibirNumeros();
}
}