Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
22 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
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 |
---|---|---|
@@ -1,16 +1,43 @@ | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
public class DadoGenerico<T> implements Sorteador<T> { | ||
|
||
Map<T, Integer> frequenciaByResultado; | ||
private Map<T, Integer> frequenciaByResultado; | ||
|
||
private int somaDasFrequenciasRelativas; | ||
|
||
private Random random; | ||
|
||
public DadoGenerico(Map<T, Integer> frequenciaByResultado) { | ||
this.frequenciaByResultado = frequenciaByResultado; | ||
|
||
this.somaDasFrequenciasRelativas = 0; | ||
for (int freq : frequenciaByResultado.values()) { | ||
somaDasFrequenciasRelativas += freq; | ||
} | ||
|
||
this.random = new Random(); | ||
} | ||
|
||
@Override | ||
public T sortear() { | ||
// ToDo IMPLEMENT ME!!!! | ||
|
||
double numeroSorteado = this.random.nextDouble() * this.somaDasFrequenciasRelativas; | ||
|
||
int freqAcumulada = 0; | ||
|
||
for (Map.Entry<T, Integer> resultadoEFrequencia : this.frequenciaByResultado.entrySet()) { | ||
T resultado = resultadoEFrequencia.getKey(); | ||
int frequenciaRelativa = resultadoEFrequencia.getValue(); | ||
|
||
freqAcumulada += frequenciaRelativa; | ||
|
||
if (numeroSorteado < freqAcumulada) { | ||
return resultado; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,54 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class DadoGenericoTest { | ||
|
||
@Test | ||
public void testarFuncionamento() { | ||
|
||
Map<String, Integer> mapinha = new HashMap<>(); | ||
mapinha.put("pedra", 2); | ||
mapinha.put("papel", 3); | ||
mapinha.put("tesoura", 10); | ||
|
||
DadoGenerico<String> dadoGenerico = new DadoGenerico<>(mapinha); | ||
|
||
int contPedra = 0; | ||
int contPapel = 0; | ||
int contTesoura = 0; | ||
|
||
|
||
final int TOTAL_SORTEIOS = 10_000; | ||
|
||
for (int i = 0; i < TOTAL_SORTEIOS; i++) { | ||
final String resultado = dadoGenerico.sortear(); | ||
|
||
System.out.println(resultado); | ||
|
||
switch (resultado) { | ||
case "pedra": | ||
contPedra++; | ||
break; | ||
case "papel": | ||
contPapel++; | ||
break; | ||
case "tesoura": | ||
contTesoura++; | ||
break; | ||
} | ||
} | ||
|
||
final double frequenciaPedra = contPedra / (double) TOTAL_SORTEIOS; | ||
final double frequenciaPapel = contPapel / (double) TOTAL_SORTEIOS; | ||
final double frequenciaTesoura = contTesoura / (double) TOTAL_SORTEIOS; | ||
|
||
|
||
assertTrue(Math.abs(frequenciaPedra - 2/15.0) < 0.01); | ||
assertTrue(Math.abs(frequenciaPapel - 3/15.0) < 0.01); | ||
assertTrue(Math.abs(frequenciaTesoura - 10/15.0) < 0.01); | ||
} | ||
} |
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
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,18 @@ | ||
public class SorteadorViaDoisParesConsecutivos implements Sorteador<Integer> { | ||
|
||
private DadoGenerico<Integer> dado; | ||
|
||
public SorteadorViaDoisParesConsecutivos(DadoGenerico<Integer> dado) { | ||
this.dado = dado; | ||
} | ||
|
||
@Override | ||
public Integer sortear() { | ||
int resultadoA = this.dado.sortear(); | ||
int resultadoB = this.dado.sortear(); | ||
int resultadoC = this.dado.sortear(); | ||
int resultadoD = this.dado.sortear(); | ||
|
||
return resultadoA == resultadoB && resultadoC == resultadoD ? 1 : 0; | ||
} | ||
} |
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 @@ | ||
public class SorteadorViaTrio implements Sorteador<Integer> { | ||
|
||
private DadoGenerico<Integer> dado; | ||
|
||
public SorteadorViaTrio(DadoGenerico<Integer> dado) { | ||
this.dado = dado; | ||
} | ||
|
||
@Override | ||
public Integer sortear() { | ||
int resultadoA = this.dado.sortear(); | ||
int resultadoB = this.dado.sortear(); | ||
int resultadoC = this.dado.sortear(); | ||
|
||
return resultadoA == resultadoB && resultadoA == resultadoC ? 1 : 0; | ||
} | ||
} |