Created
November 14, 2013 16:57
-
-
Save adolfont/7470304 to your computer and use it in GitHub Desktop.
Dojo de 14/11/2013
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
package utfpr; | |
import static org.junit.Assert.*; | |
import java.lang.*; | |
import org.junit.Test; | |
public class PalavrasPrimasTest { | |
@Test | |
public void testaVazio() | |
{ | |
assertEquals(false,PalavrasPrimas.verificaPalavraPrima("")); | |
} | |
@Test | |
public void testaUmCharNaoPrimo(){ | |
assertEquals(false ,PalavrasPrimas.verificaPalavraPrima("a")); | |
} | |
@Test | |
public void testaUmCharPrimo(){ | |
assertEquals(true ,PalavrasPrimas.verificaPalavraPrima("b")); | |
} | |
@Test | |
public void testaConversaoMinusculo() { | |
assertEquals(1, PalavrasPrimas.converteChar('a')); | |
assertEquals(2, PalavrasPrimas.converteChar('b')); | |
} | |
@Test | |
public void testaConversaoMaiusculo() { | |
assertEquals(27, PalavrasPrimas.converteChar('A')); | |
assertEquals(28, PalavrasPrimas.converteChar('B')); | |
} | |
@Test | |
public void testaNumeroPrimo() { | |
assertEquals(true, PalavrasPrimas.testaPrimo(2)); | |
assertEquals(true, PalavrasPrimas.testaPrimo(13)); | |
} | |
@Test | |
public void testaNaoNumeroPrimo() { | |
assertEquals(false, PalavrasPrimas.testaPrimo(4)); | |
assertEquals(false, PalavrasPrimas.testaPrimo(8)); | |
assertEquals(false, PalavrasPrimas.testaPrimo(1)); | |
assertEquals(false, PalavrasPrimas.testaPrimo(42)); | |
} | |
@Test | |
public void testaPalavraPrima() { | |
assertEquals(true, PalavrasPrimas.verificaPalavraPrima("ab")); | |
} | |
@Test | |
public void testaPalavraNaoPrima() { | |
assertEquals(false, PalavrasPrimas.verificaPalavraPrima("bb")); | |
} | |
@Test(expected=IllegalArgumentException.class) | |
public void testaFalsaPalavra(){ | |
assertEquals(false, PalavrasPrimas.verificaPalavraPrima("a + b")); | |
} | |
} | |
============ | |
package utfpr; | |
public class PalavrasPrimas { | |
public static boolean verificaPalavraPrima(String string) { | |
if (string == "") | |
return false; | |
int aux = 0; | |
for (int i = 0; i < string.length(); i++) { | |
aux += converteChar(string.charAt(i)); | |
if (!Character.isLetter(string.charAt(i))) { | |
throw new IllegalArgumentException(); | |
} | |
} | |
return testaPrimo(aux); | |
} | |
public static int converteChar(char c) { | |
if (c > 96) { // se for maior que 96 é minúsculo | |
return c - 96; | |
} else { | |
return c - 38; | |
} | |
} | |
public static boolean testaPrimo(int i) { | |
if (i == 1) | |
return false; | |
for (int j = 2; j <= Math.sqrt(i); j++) { | |
if (i % j == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problema: http://dojopuzzles.com/problemas/exibe/palavras-primas/