Created
May 10, 2017 03:23
-
-
Save delucas/b1622618f511e95a1522f79835ca9be4 to your computer and use it in GitHub Desktop.
Pequeño ejemplo de cómo probar código que necesita números aleatorios generados. Java, UNLaM. 2017
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
import java.util.Random; | |
public class ActualRandomGenerator extends RandomGenerator { | |
@Override | |
public int obtenerAleatorioMenorQue(int maximo) { | |
return new Random().nextInt(maximo); | |
} | |
} |
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
import java.util.LinkedList; | |
import java.util.List; | |
public class Cubilete { | |
private List<Dado> dados = new LinkedList<Dado>(); | |
public void agregar(Dado dado) { | |
this.dados .add(dado); | |
} | |
public int tirar() { | |
int resultado = 0; | |
for (Dado dado : dados) { | |
resultado += dado.tirar(); | |
} | |
return resultado; | |
} | |
} |
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
public class Dado { | |
private RandomGenerator aleatorizador; | |
public Dado() { | |
this.aleatorizador = new ActualRandomGenerator(); | |
} | |
public void setRandomGenerator(RandomGeneratorStub randomGeneratorStub) { | |
this.aleatorizador = randomGeneratorStub; | |
} | |
public int tirar() { | |
return this.aleatorizador.obtenerAleatorioMenorQue(6); | |
} | |
} |
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
import org.junit.Assert; | |
import org.junit.Test; | |
public class DadoTests { | |
@Test | |
public void queSumaCorrectamenteLosValores() { | |
Dado dado1 = new Dado(); | |
dado1.setRandomGenerator(new RandomGeneratorStub(5)); | |
Dado dado2 = new Dado(); | |
dado2.setRandomGenerator(new RandomGeneratorStub(2)); | |
Cubilete cubilete = new Cubilete(); | |
cubilete.agregar(dado1); | |
cubilete.agregar(dado2); | |
Assert.assertEquals(5 + 2, cubilete.tirar()); | |
} | |
} |
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
public abstract class RandomGenerator { | |
public abstract int obtenerAleatorioMenorQue(int maximo); | |
} |
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
public class RandomGeneratorStub extends RandomGenerator { | |
private int valor; | |
public RandomGeneratorStub(int valorFijo) { | |
this.valor = valorFijo; | |
} | |
@Override | |
public int obtenerAleatorioMenorQue(int maximo) { | |
return this.valor; // aqui esta el truco | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CSS Base style for Doctorisy
Changelog
Versions- 0.1.0. First version of the base style project, with elements and colors from the Doctorisy design guide
- 0.2.0. Second version of base style project, with missing elements, colors and sizes according to the Doctorisy style guide