-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ultimos programas - Arvores binarias, AVL, grafos, recursividade e pa…
…radigmas
- Loading branch information
Showing
34 changed files
with
1,506 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ED1.Arrays; | ||
|
||
public class EntradaJogo { | ||
protected String nome; | ||
protected int score; | ||
|
||
public EntradaJogo(String nome, int score) { | ||
this.nome = nome; | ||
this.score = score; | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public int getScore() { | ||
return score; | ||
} | ||
|
||
public String toString(){ | ||
return "[" + this.nome + " - " + this.score + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ED1.Arrays; | ||
|
||
public class Scores { | ||
|
||
public static final int maximoEntradas = 10; //quantidade de escores que serão armazenados | ||
protected int numEntradas; //número real de registros | ||
protected EntradaJogo[] placares; | ||
|
||
public Scores() { | ||
placares = new EntradaJogo[maximoEntradas]; | ||
numEntradas = 0; | ||
} | ||
public String toString(){ | ||
StringBuilder s = new StringBuilder("["); | ||
for (int i = 0; i < numEntradas; i++) { | ||
if(i>0) s.append(", "); | ||
s.append(placares[i]); | ||
} | ||
return s + "]"; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ED1.Arrays; | ||
|
||
public class TabelaScores { | ||
|
||
|
||
private int numEntries = 0; | ||
|
||
private EntradaJogo[] board; //array of game entries(names & scores) | ||
|
||
public TabelaScores(int capacity){ | ||
this.board = new EntradaJogo[capacity]; | ||
} | ||
|
||
public void add(EntradaJogo e){ | ||
int newScore = e.getScore(); | ||
|
||
if(numEntries < board.length || newScore > board[numEntries-1].getScore()){ | ||
if(numEntries < board.length) // no score drops from the board | ||
numEntries++; //so overall number increases | ||
} | ||
|
||
int j = numEntries - 1; | ||
while(j > 0 && board[j-1].getScore() < newScore){ | ||
board[j] = board[j-1]; //shift entry from j-1 to j | ||
j -- ; //decrement | ||
} | ||
|
||
board[j] = e; //when done add new entry | ||
} | ||
|
||
public EntradaJogo remove(int i) throws IndexOutOfBoundsException{ | ||
if (i < 0 || i >= numEntries){ // If index is less than 0 or index is greater than or equal than numEntries | ||
throw new IndexOutOfBoundsException("invalid index:" + i); // throw index out of bound exception | ||
|
||
} | ||
EntradaJogo temp = board[i]; // create temp variable to return | ||
|
||
for(int j = i; j < numEntries - 1; j++){ //for-loop through | ||
board[j] = board[j+1]; | ||
board[numEntries - 1 ] = null; | ||
numEntries--; | ||
|
||
} | ||
return temp; | ||
} | ||
|
||
public String toString(){ | ||
StringBuilder s = new StringBuilder("["); | ||
for (int i = 0; i < numEntries; i++) { | ||
if(i>0) s.append(", \n"); | ||
s.append(board[i]); | ||
} | ||
return s + "]"; | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
UNP/ref/Java/#ED2-Java-BKP/ED1/Arrays/TestaTabelaScores.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ED1.Arrays; | ||
|
||
public class TestaTabelaScores { | ||
public static void main(String[] args) { | ||
TabelaScores tabela = new TabelaScores(10); | ||
tabela.add(new EntradaJogo("Joeliton", 100)); | ||
tabela.add(new EntradaJogo("Mariana", 200)); | ||
tabela.add(new EntradaJogo("Joseph", 300)); | ||
tabela.add(new EntradaJogo("Pietro", 400)); | ||
tabela.add(new EntradaJogo("Alice", 500)); | ||
tabela.add(new EntradaJogo("João", 600)); | ||
tabela.add(new EntradaJogo("Maria", 700)); | ||
tabela.add(new EntradaJogo("Jose", 800)); | ||
tabela.add(new EntradaJogo("Pedro", 900)); | ||
tabela.add(new EntradaJogo("Ana", 1000)); | ||
tabela.add(new EntradaJogo("Carlos", 1100)); | ||
System.out.println("Placares do jogo\n============================"); | ||
System.out.println(tabela); | ||
tabela.remove(7); | ||
System.out.println("Placares do jogo\n============================"); | ||
System.out.println(tabela); | ||
|
||
|
||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
UNP/ref/Java/#ED2-Java-BKP/EstruturasBasicas/EncadeadaPrimeiro/Lista.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package EstruturasBasicas.EncadeadaPrimeiro; | ||
|
||
public class Lista { | ||
No primeiro; | ||
No ultimo; | ||
int tamanho; | ||
|
||
public Lista() { | ||
this.tamanho = 0; | ||
} | ||
|
||
// Método para adicionar um elemento na lista | ||
public void adicionar(String info) { | ||
No novoNo = new No(); | ||
novoNo.info = info; | ||
novoNo.proximo = null; | ||
|
||
if (primeiro == null) { | ||
primeiro = novoNo; | ||
ultimo = novoNo; | ||
} else { | ||
ultimo.proximo = novoNo; | ||
ultimo = novoNo; | ||
} | ||
|
||
tamanho++; | ||
} | ||
|
||
// Método para imprimir a lista | ||
public void imprimir() { | ||
No atual = primeiro; | ||
|
||
while (atual != null) { | ||
System.out.println(atual.info); | ||
atual = atual.proximo; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
UNP/ref/Java/#ED2-Java-BKP/EstruturasBasicas/EncadeadaPrimeiro/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package EstruturasBasicas.EncadeadaPrimeiro; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
Lista lista = new Lista(); | ||
|
||
// Adicionando elementos | ||
lista.adicionar("Elemento 1"); | ||
lista.adicionar("Elemento 2"); | ||
lista.adicionar("Elemento 3"); | ||
//lista.adicionar(2); | ||
|
||
// Imprimindo a lista | ||
lista.imprimir(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
UNP/ref/Java/#ED2-Java-BKP/EstruturasBasicas/EncadeadaPrimeiro/No.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package EstruturasBasicas.EncadeadaPrimeiro; | ||
|
||
public class No { | ||
String info; | ||
No proximo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,5 +153,7 @@ public static void main(String[] args) throws Exception { | |
pulaLinha(1); | ||
fila.imprime(); | ||
pulaLinha(1); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
UNP/ref/Java/#ED2-Java-BKP/EstruturasBasicas/Pilha/PilhaLivroTeste.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package EstruturasBasicas.Pilha; | ||
|
||
import java.util.EmptyStackException; | ||
|
||
// Definição do TAD Pilha | ||
public class PilhaLivroTeste<T> { | ||
private Celula<T> top; | ||
|
||
// Definição da estrutura do nó da pilha | ||
private static class Celula<T> { | ||
private T info; | ||
private Celula<T> proximo; | ||
|
||
public Celula(T info) { | ||
this.info = info; | ||
} | ||
} | ||
|
||
// Operação para verificar se a pilha está vazia | ||
public boolean estaVazia() { | ||
return top == null; | ||
} | ||
|
||
// Operação para adicionar um elemento no topo da pilha | ||
public void push(T info) { | ||
Celula<T> novaCelula = new Celula<T>(info); | ||
novaCelula.proximo = top; | ||
top = novaCelula; | ||
} | ||
|
||
// Operação para remover e retornar o elemento no topo da pilha | ||
public T pop() { | ||
if (estaVazia()) { | ||
throw new EmptyStackException(); | ||
} | ||
T info = top.info; | ||
top = top.proximo; | ||
return info; | ||
} | ||
|
||
// Operação para consultar o elemento no topo da pilha | ||
public T peek() { | ||
if (estaVazia()) { | ||
throw new EmptyStackException(); | ||
} | ||
return top.info; | ||
} | ||
|
||
public static void main(String[] args) { | ||
PilhaLivroTeste<Integer> Pilha = new PilhaLivroTeste<Integer>(); | ||
|
||
Pilha.push(3); | ||
Pilha.push(5); | ||
Pilha.push(7); | ||
|
||
System.out.println("Pilha: "); | ||
while (!Pilha.estaVazia()) { | ||
System.out.println(Pilha.pop()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package TestaTrabalhos; | ||
|
||
public class Testa { | ||
|
||
public static double calculateSum(int n) { | ||
|
||
if (n == 1) { | ||
return 1.0; | ||
} else { | ||
return calculateSum(n - 1) + 1.0 / n; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
int n = 5; // Change this value to the desired number of terms | ||
double sum = calculateSum(n); | ||
System.out.println("Sum of the series: " + sum); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.